Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Tutorial

Python

Python tutorial provides basic and advanced concepts of Python. Our Python tutorial is designed for beginners and professionals.

Python is a simple, general purpose, high level, and object-oriented programming language.

Python is an interpreted scripting language also. Guido Van Rossum is known as the founder of Python programming.

Our Python tutorial includes all topics of Python Programming such as installation, control statements, Strings, Lists, Tuples, Dictionary, Modules, Exceptions, Date and Time, File I/O, Programs, etc. There are also given Python interview questions to help you better understand Python Programming.

What is Python

Python is a general purpose, dynamic, high-level, and interpreted programming language. It supports Object Oriented programming approach to develop applications. It is simple and easy to learn and provides lots of high-level data structures.

Python is easy to learn yet powerful and versatile scripting language, which makes it attractive for Application Development.

Python's syntax and dynamic typing with its interpreted nature make it an ideal language for scripting and rapid application development.

Python supports multiple programming pattern, including object-oriented, imperative, and functional or procedural programming styles.

Python is not intended to work in a particular area, such as web programming. That is why it is known as multipurpose programming language because it can be used with web, enterprise, 3D CAD, etc.

We don't need to use data types to declare variable because it is dynamically typed so we can write a=10 to assign an integer value in an integer variable.

Python makes the development and debugging fast because there is no compilation step included in Python development, and edit-test-debug cycle is very fast.

Python 2 vs. Python 3

In most of the programming languages, whenever a new version releases, it supports the features and syntax of the existing version of the language, therefore, it is easier for the projects to switch in the newer version. However, in the case of Python, the two versions Python 2 and Python 3 are very much different from each other.

A list of differences between Python 2 and Python 3 are given below:

  1. Python 2 uses print as a statement and used as print "something" to print some string on the console. On the other hand, Python 3 uses print as a function and used as print("something") to print something on the console.
  2. Python 2 uses the function raw_input() to accept the user's input. It returns the string representing the value, which is typed by the user. To convert it into the integer, we need to use the int() function in Python. On the other hand, Python 3 uses input() function which automatically interpreted the type of input entered by the user. However, we can cast this value to any type by using primitive functions (int(), str(), etc.).
  3. In Python 2, the implicit string type is ASCII, whereas, in Python 3, the implicit string type is Unicode.
  4. Python 3 doesn't contain the xrange() function of Python 2. The xrange() is the variant of range() function which returns a xrange object that works similar to Java iterator. The range() returns a list for example the function range(0,3) contains 0, 1, 2.
  5. There is also a small change made in Exception handling in Python 3. It defines a keyword as which is necessary to be used. We will discuss it in Exception handling section of Python programming tutorial.

Python History

Python was invented by Guido van Rossum in 1991 at CWI in Netherland. The idea of Python programming language has taken from the ABC programming language or we can say that ABC is a predecessor of Python language.

There is also a fact behind the choosing name Python. Guido van Rossum was a fan of the popular BBC comedy show of that time, "Monty Python's Flying Circus". So he decided to pick the name Python for his newly created programming language.

Python has the vast community across the world and releases its version within the short period.

Why learn Python?

Python provides many useful features to the programmer. These features make it most popular and widely used language. We have listed below few-essential feature of Python.

  • Easy to use and Learn
  • Expressive Language
  • Interpreted Language
  • Object-Oriented Language
  • Open Source Language
  • Extensible
  • Learn Standard Library
  • GUI Programming Support
  • Integrated
  • Embeddable
  • Dynamic Memory Allocation
  • Wide Range of Libraries and Frameworks

Where is Python used?

Python is a general-purpose, popular programming language and it is used in almost every technical field. The various areas of Python use are given below.

  • Data Science
  • Date Mining
  • Desktop Applications
  • Console-based Applications
  • Mobile Applications
  • Software Development
  • Artificial Intelligence
  • Web Applications
  • Enterprise Applications
  • 3D CAD Applications
  • Machine Learning
  • Computer Vision or Image Processing Applications.
  • Speech Recognitions

Python Basic Syntax

There is no use of curly braces or semicolon in Python programming language. It is English-like language. But Python uses the indentation to define a block of code. Indentation is nothing but adding whitespace before the statement when it is needed. For example -

  1. def func():  
  2.        statement 1  
  3.        statement 2  
  4.        …………………  
  5.        …………………  
  6.          statement N  

In the above example, the statements that are same level to right belong to the function. Generally, we can use four whitespaces to define indentation.

Python First Program

Unlike the other programming languages, Python provides the facility to execute the code using few lines. For example - Suppose we want to print the "Hello World" program in Java; it will take three lines to print it.

  1. public class HelloWorld {  
  2.  public static void main(String[] args){  
  3. // Prints "Hello, World" to the terminal window.  
  4.   System.out.println("Hello World");  
  5.  }  
  6.  }  

On the other hand, we can do this using one statement in Python.

  1. print("Hello World")  

Both programs will print the same result, but it takes only one statement without using a semicolon or curly braces in Python.

Python Popular Frameworks and Libraries

Python has wide range of libraries and frameworks widely used in various fields such as machine learning, artificial intelligence, web applications, etc. We define some popular frameworks and libraries of Python as follows.

  • Web development (Server-side) - Django Flask, Pyramid, CherryPy
  • GUIs based applications - Tk, PyGTK, PyQt, PyJs, etc.
  • Machine Learning - TensorFlow, PyTorch, Scikit-learn, Matplotlib, Scipy, etc.
  • Mathematics - Numpy, Pandas, etc.

Python print() Function

The print() function displays the given object to the standard output device (screen) or to the text stream file.

Unlike the other programming languages, Python print() function is most unique and versatile function.

The syntax of print() function is given below.

  1. print(*objects, sep=' ', end='\n', file=sys.stdout, flush=False)  

Let's explain its parameters one by one.

  • objects - An object is nothing but a statement that to be printed. The * sign represents that there can be multiple statements.
  • sep - The sep parameter separates the print values. Default values is ' '.
  • end - The end is printed at last in the statement.
  • file - It must be an object with a write(string) method.
  • flush - The stream or file is forcibly flushed if it is true. By default, its value is false.

Let's understand the following example.

Example - 1: Return a value

  1. print("Welcome to javaTpoint.")  
  2.   
  3. a = 10  
  4. # Two objects are passed in print() function  
  5. print("a =", a)  
  6.   
  7. b = a  
  8. # Three objects are passed in print function  
  9. print('a =', a, '= b')  

Output:

Welcome to javaTpoint.
a = 10
a = 10 = b

As we can see in the above output, the multiple objects can be printed in the single print() statement. We just need to use comma (,) to separate with each other.

Example - 2: Using sep and end argument

  1. a = 10  
  2. print("a =", a, sep='dddd', end='\n\n\n')  
  3. print("a =", a, sep='0', end='$$$$$')  

Output:

a =dddd10


a =010$$$$$

In the first print() statement, we use the sep and end arguments. The given object is printed just after the sep values. The value of end parameter printed at the last of given object. As we can see that, the second print() function printed the result after the three black lines.

Taking Input to the User

Python provides the input() function which is used to take input from the user. Let's understand the following example.

Example -

  1. name = input("Enter a name of student:")  
  2. print("The student name is: ", name)  

Output:

Enter a name of student: Devansh
The student name is: Devansh

By default, the input() function takes the string input but what if we want to take other data types as an input.

If we want to take input as an integer number, we need to typecast the input() function into an integer.

For example -

Example -

  1. a  = int(input("Enter first number: "))  
  2. b = int(input("Enter second number: "))  
  3.   
  4. print(a+b)  

Output:

Enter first number: 50
Enter second number: 100
150

We can take any type of values using input() function.

Python Operators

Operators are the symbols which perform various operations on Python objects. Python operators are the most essential to work with the Python data types. In addition, Python also provides identify membership and bitwise operators. We will learn all these operators with the suitable example in following tutorial.

  • Python Operators

Python Conditional Statements

Conditional statements help us to execute a particular block for a particular condition. In this tutorial, we will learn how to use the conditional expression to execute a different block of statements. Python provides if and else keywords to set up logical conditions. The elif keyword is also used as conditional statement.

  • Python if..else statement

Python Loops

Sometimes we may need to alter the flow of the program. The execution of a specific code may need to be repeated several numbers of times. For this purpose, the programming languages provide various types of loops capable of repeating some specific code several times. Consider the following tutorial to understand the statements in detail.

  • Python Loops
  • Python For Loop
  • Python While Loop

Python Data Structures

Data structures are referred which can hold some data together or we say that they are used to store the data in organized way. Python provides built-in data structures such as list, tuple, dictionary, and set. We can perform complex tasks using data structures.

Python List

Python list holds the ordered collection of items. We can store a sequence of items in a list. Python list is mutable which means it can be modified after its creation. The items of lists are enclosed within the square bracket [] and separated by the comma. Let's see the example of list.

  1. L1 = ["John"102"USA"]      
  2. L2 = [123456]     

If we try to print the type of L1, L2, and L3 using type() function then it will come out to be a list.

  1. print(type(L1))    
  2. print(type(L2))    

Output:

<class 'list'>
<class 'list'>

To learn more about list, visit the following tutorial.

  • Python List
  • Python List Functions

Python Tuple

Python Tuple is used to store the sequence of immutable Python objects. The tuple is similar to lists since the value of the items stored in the list can be changed, whereas the tuple is immutable, and the value of the items stored in the tuple cannot be changed.

Tuple can be defined as follows

Example -

  1. tup = ("Apple""Mango" , "Orange" , "Banana")  
  2. print(type(tup))  
  3. print(tup)  

Output:

<class 'tuple'>
('Apple', 'Mango', 'Orange', 'Banana')

If we try to add new to the tuple, it will throw an error.

Example -

  1. tup = ("Apple""Mango" , "Orange" , "Banana")  
  2.   
  3. tup[2] = "Papaya"  
  4. print(tup)  

Output:

Traceback (most recent call last):
File "C:/Users/DEVANSH SHARMA/PycharmProjects/Hello/gamewithturtle.py", line 3, in 
tup[2] = "Papaya"
TypeError: 'tuple' object does not support item assignment

The above program throws an error because tuples are immutable type. To learn more about tuple, visit the Python Tuples.

  • Python Tuple

Python String

Python string is a sequence of characters. It is a collection of the characters surrounded by single quotes, double quotes, or triple quotes. It can also define as collection of the Unicode characters. We can create a string as follows.

Example -

  1. # Creating string using double quotes  
  2. str1 = "Hi Python"  
  3. print(str1)  
  4. # Creating string using single quotes  
  5. str1 = 'Hi Python'  
  6. print(str1)  
  7. # Creating string using triple quotes  
  8. str1 = '''Hi Python'''  
  9. print(str1)  

Output:

Hi Python
Hi Python
Hi Python

Python doesn't support the character data-type. A single character written as 'p' is treated as a string of length 1.

Stings are also immutable. We can't change after it is declared. To learn more about the string, visit the following tutorial.

  • Python Strings
  • Python String Method

Dictionaries

Python Dictionary is a most efficient data structure and used to store the large amount of data. It stores the data in the key-value pair format. Each value is stored corresponding to its key.

Keys must be a unique and value can be any type such as integer, list, tuple, etc.

It is a mutable type; we can reassign after its creation. Below is the example of creating dictionary in Python.

Example -

  1. employee = {"Name""John""Age"29"salary":250000,"Company":"GOOGLE"}      
  2. print(type(employee))      
  3. print("printing Employee data .... ")      
  4. print(employee)  

Output:

<class 'dict'>
Printing Employee data ....
{'Name': 'John', 'Age': 29, 'salary': 250000, 'Company': 'GOOGLE'}

The empty curly braces {} are used to create empty dictionary. To learn more, visit the complete tutorial of the dictionary.

  • Python Dictionary
  • Python Dictionary Methods

Python Sets

A Python set is a collection of unordered elements. Each element in set must be unique and immutable. Sets are mutable which means we can modify anytime throughout the program. Let's understand the example of creating set in Python.

Example -

  1. # Creating Set  
  2. Month = {"January""February""March""April""May""June""July"}  
  3. print(Month)  
  4. print(type(Month))  

Output:

{'March', 'July', 'April', 'May', 'June', 'February', 'January'}
<class 'set'>

To get the more information about sets, visit the following resources.

  • Python Sets
  • Python Set Methods

Python Functional Programming

This section of Python tutorial defines some important tools related to functional programming such as lambda and recursive functions. These functions are very efficient in accomplishing the complex tasks. We define a few important functions, such as reduce, map, and filter. Python provides the functools module that includes various functional programming tools. Visit the following tutorial to learn more about functional programming.

  • Python Function
  • Python map() Function
  • Python filter() Function
  • Python reduce() Function
  • Python functool Module
  • Python Lambda Function

Python File I/O

Files are used to store data in a computer disk. In this tutorial, we explain the built-in file object of Python. We can open a file using Python script and perform various operations such as writing, reading, and appending. There are various ways of opening a file. We are explained with the relevant example. We will also learn to perform read/write operations on binary files.

  • Python File I/O

Python Modules

Python modules are the program files that contain a Python code or functions. There are two types of module in the Python - User-define modules and built-in modules. A module that the user defines, or we can say that our Python code saved with .py extension, is treated as a user-define module.

Built-in modules are predefined modules of Python. To use the functionality of the modules, we need to import them into our current working program.

  • Python Modules

Python Exceptions

An exception can be defined as an unusual condition in a program resulting in the interruption in the flow of the program.

Whenever an exception occurs, the program stops the execution, and thus the further code is not executed. Therefore, an exception is the run-time errors that are unable to handle to Python script. An exception is a Python object that represents an error.

  • Python Exceptions

Python CSV

A csv stands for "comma separated values", which is defined as a simple file format that uses specific structuring to arrange tabular data. It stores tabular data such as spreadsheet or database in plain text and has a common format for data interchange. A csv file opens into the excel sheet, and the rows and columns data define the standard format. Visit the following tutorial to learn the CSV module in detail.

  • Python Read CSV File
  • Python Write CSV File

Python Sending Mail

We can send or read a mail using the Python script. Python's standard library modules are useful for handling various protocols such as PoP3 and IMAP. We will learn how to send a mail with the popular email service SMTP from a Python script.

  • Python Sending Emails

Python Magic Methods

Python magic method is defined as the special method which adds "magic" to a class. It starts and ends with double underscores, for example, _init_ or _str_.

The built-in classes define many magic methods. The dir() function can be used to see the number of magic methods inherited by a class. It has two prefixes and suffix underscores in the method name.

  • Python Magic Methods

Python Oops Concepts

Everything in Python is treated as an object including integer values, floats, functions, classes, and none. Apart from that, Python supports all oriented concepts. Below is the brief introduction of oops concepts of Python.

  • Classes and Objects - Python classes are the blueprint of the object. An object is a collection of data and method that act on the data.
  • Inheritance - An inheritance is a technique where one class inherits the properties of other classes.
  • Constructor - Python provides a special method __init__() which is known as a constructor. This method is automatically called when an object is instantiated.
  • Data Member - A variable that holds data associated with a class and its objects.

To read the oops concept in detail, visit the following resources.

  • Python Oops Concepts
  • Python Object and classes
  • Python Constructor
  • Python Inheritance
  • Python Polymorphism

Python Advance Topics

Python includes many advance and useful concepts that help the programmer to solve the complex tasks. These concepts are given below.

Python Iterator

An iterator is simply an object that can be iterated upon. It returns one object at a time. It can be implemented using the two special methods, __iter__() and __next__().

To learn more about the iterators visit our Python Iterators tutorial.

Python Generators

The Generators are an easiest way of creating Iterators. To learn more about, visit our Python Generators tutorial.

Python Decorators

These are used to modify the behavior of the function. Decorators provide the flexibility to wrap another function to expand the working of wrapped function, without permanently modifying it.

To learn more about, visit the Python Decorators tutorial.

Python Database Connections

We can use various databases along with Python. You can learn the full tutorial to visit below resources. Python DBI-API acclaims standard sets of functionality to be included in the database connectivity modules for respective RDBMS products. We explain all important database connectivity using Python DBI-API.

Python MySQL

Environment Setup

Database Connection

Creating New Database

Creating Tables

Insert Operation

Read Operation

Update Operation

Join Operation

Performing Transactions

Python MongoDB

Python MongoDB

Python SQLite

Python SQLite

Python CGI

Python CGI stands for "Common Gateway Interface", which is used to define how to exchange information between the webserver and a custom Python scripts. The Common Gateway Interface is a standard for external gateway programs to interface with the server, such as HTTP Servers. To learn more about Python CGI, visit the following tutorial.

  • Python CGI

Prerequisite

Before learning Python, you must have the basic knowledge of programming concepts.

Audience

Our Python tutorial is designed to help beginners and professionals.

Problem

We assure that you will not find any problem in this Python tutorial. But if there is any mistake, please post the problem in contact form.


Next TopicFeatures of Python





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Features

Python provides many useful features which make it popular and valuable from the other programming languages. It supports object-oriented programming, procedural programming approaches and provides dynamic memory allocation. We have listed below a few essential features.

1) Easy to Learn and Use

Python is easy to learn as compared to other programming languages. Its syntax is straightforward and much the same as the English language. There is no use of the semicolon or curly-bracket, the indentation defines the code block. It is the recommended programming language for beginners.

2) Expressive Language

Python can perform complex tasks using a few lines of code. A simple example, the hello world program you simply type print("Hello World"). It will take only one line to execute, while Java or C takes multiple lines.

3) Interpreted Language

Python is an interpreted language; it means the Python program is executed one line at a time. The advantage of being interpreted language, it makes debugging easy and portable.

4) Cross-platform Language

Python can run equally on different platforms such as Windows, Linux, UNIX, and Macintosh, etc. So, we can say that Python is a portable language. It enables programmers to develop the software for several competing platforms by writing a program only once.

5) Free and Open Source

Python is freely available for everyone. It is freely available on its official website www.python.org. It has a large community across the world that is dedicatedly working towards make new python modules and functions. Anyone can contribute to the Python community. The open-source means, "Anyone can download its source code without paying any penny."

6) Object-Oriented Language

Python supports object-oriented language and concepts of classes and objects come into existence. It supports inheritance, polymorphism, and encapsulation, etc. The object-oriented procedure helps to programmer to write reusable code and develop applications in less code.

7) Extensible

It implies that other languages such as C/C++ can be used to compile the code and thus it can be used further in our Python code. It converts the program into byte code, and any platform can use that byte code.

8) Large Standard Library

It provides a vast range of libraries for the various fields such as machine learning, web developer, and also for the scripting. There are various machine learning libraries, such as Tensor flow, Pandas, Numpy, Keras, and Pytorch, etc. Django, flask, pyramids are the popular framework for Python web development.

9) GUI Programming Support

Graphical User Interface is used for the developing Desktop application. PyQT5, Tkinter, Kivy are the libraries which are used for developing the web application.

10) Integrated

It can be easily integrated with languages like C, C++, and JAVA, etc. Python runs code line by line like C,C++ Java. It makes easy to debug the code.

11. Embeddable

The code of the other programming language can use in the Python source code. We can use Python source code in another programming language as well. It can embed other language into our code.

12. Dynamic Memory Allocation

In Python, we don't need to specify the data-type of the variable. When we assign some value to the variable, it automatically allocates the memory to the variable at run time. Suppose we are assigned integer value 15 to x, then we don't need to write int x = 15. Just write x = 15.


Next TopicPython History





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python History and Versions

  • Python laid its foundation in the late 1980s.
  • The implementation of Python was started in December 1989 by Guido Van Rossum at CWI in Netherland.
  • In February 1991, Guido Van Rossum published the code (labeled version 0.9.0) to alt.sources.
  • In 1994, Python 1.0 was released with new features like lambda, map, filter, and reduce.
  • Python 2.0 added new features such as list comprehensions, garbage collection systems.
  • On December 3, 2008, Python 3.0 (also called "Py3K") was released. It was designed to rectify the fundamental flaw of the language.
  • ABC programming language is said to be the predecessor of Python language, which was capable of Exception Handling and interfacing with the Amoeba Operating System.
  • The following programming languages influence Python:
    • ABC language.
    • Modula-3

Why the Name Python?

There is a fact behind choosing the name Python. Guido van Rossum was reading the script of a popular BBC comedy series "Monty Python's Flying Circus". It was late on-air 1970s.

Van Rossum wanted to select a name which unique, sort, and little-bit mysterious. So he decided to select naming Python after the "Monty Python's Flying Circus" for their newly created programming language.

The comedy series was creative and well random. It talks about everything. Thus it is slow and unpredictable, which made it very interesting.

Python is also versatile and widely used in every technical field, such as Machine Learning, Artificial Intelligence, Web Development, Mobile Application, Desktop Application, Scientific Calculation, etc.

Python Version List

Python programming language is being updated regularly with new features and supports. There are lots of update in Python versions, started from 1994 to current release.

A list of Python versions with its released date is given below.

Python VersionReleased Date
Python 1.0January 1994
Python 1.5December 31, 1997
Python 1.6September 5, 2000
Python 2.0October 16, 2000
Python 2.1April 17, 2001
Python 2.2December 21, 2001
Python 2.3July 29, 2003
Python 2.4November 30, 2004
Python 2.5September 19, 2006
Python 2.6October 1, 2008
Python 2.7July 3, 2010
Python 3.0December 3, 2008
Python 3.1June 27, 2009
Python 3.2February 20, 2011
Python 3.3September 29, 2012
Python 3.4March 16, 2014
Python 3.5September 13, 2015
Python 3.6December 23, 2016
Python 3.7June 27, 2018
Python 3.8October 14, 2019

Tips to Keep in Mind While Learning Python

The most common question asked by the beginners - "What is the best way to learn Python"? It is the initial and relevant question because first step in learning any programming language is to know how to learn.

The proper way of learning will help us to learn fast and become a good Python developer.

In this section, we will discuss various tips that we should keep in mind while learning Python.

1. Make it Clear Why We Want to Learn

The goal should be clear before learning the Python. Python is an easy, a vast language as well. It includes numbers of libraries, modules, in-built functions and data structures. If the goal is unclear then it will be a boring and monotonous journey of learning Python. Without any clear goal, you perhaps won't make it done.

So, first figure out the motivation behind learning, which can anything be such as knowing something new, develop projects using Python, switch to Python, etc. Below are the general areas where Python is widely used. Pick any of them.

  • Data Analysis and Processing
  • Artificial Intelligence
  • Games
  • Hardware/Sensor/Robots
  • Desktop Applications

Choose any one or two areas according to your interest and start the journey towards learning Python.

2. Learn the Basic Syntax

It is the most essential and basic step to learn the syntax of the Python programming language. We have to learn the basic syntax before dive deeper into learning it. As we have discussed in our earlier tutorial, Python is easy to learn and has a simple syntax. It doesn't use semicolon and brackets. Its syntax is like the English language.

So it will take minimum amount of time to learning its syntax. Once we get its syntax properly, further learning will be easier and quicker getting to work on projects.

Note - Learn Python 3, not Python 2.7, because the industry no longer uses it. Our Python tutorial is based on its latest version Python 3.

3. Write Code by Own

Writing the code is the most effective and robust way to learn Python. First, try to write code on paper and run in mind (Dry Run) then move to the system. Writing code on paper will help us get familiar quickly with the syntax and the concept store in the deep memory. While writing the code, try to use proper functions and suitable variables names.

There are many editors available for Python programming which highlights the syntax related issue automatically. So we don't need to pay lot of attention of these mistakes.

4. Keep Practicing

The next important step is to do the practice. It needs to implementing the Python concepts through the code. We should be consistence to our daily coding practice.

Consistency is the key of success in any aspect of life not only in programming. Writing code daily will help to develop muscle memory.

We can do the problem exercise of related concepts or solve at least 2 or 3 problems of Python. It may seem hard but muscle memory plays large part in programing. It will take us ahead from those who believe only the reading concept of Python is sufficient.

5. Make Notes as Needed

Creating notes by own is an excellent method to learn the concepts and syntax of Python. It will establish stability and focus that helps you become a Python developer. Make brief and concise notes with relevant information and include appropriate examples of the subject concerned.

Maintain own notes are also helped to learn fast. A study published in Psychological Science that -

The students who were taking longhand notes in the studies were forced to be more selective — because you can't write as fast as you can type.

6. Discuss Concepts with Other

Coding seems to be solitary activity, but we can enhance our skills by interacting with the others. We should discuss our doubts to the expert or friends who are learning Python. This habit will help to get additional information, tips and tricks, and solution of coding problems. One of the best advantages of Python, it has a great community. Therefore, we can also learn from passionate Python enthusiasts.

7. Do small Projects

After understanding Python's basic concept, a beginner should try to work on small projects. It will help to understand Python more deeply and become more component in it. Theoretical knowledge is not enough to get command over the Python language. These projects can be anything as long as they teach you something. You can start with the small projects such as calculator app, a tic-toc-toe game, an alarm clock app, a to-do list, student or customer management system, etc.

Once you get handy with a small project, you can easily shift toward your interesting domain (Machine Learning, Web Development, etc.).

8. Teach Others

There is a famous saying that "If you want to learn something then you should teach other". It is also true in case of learning Python. Share your information to other students via creating blog posts, recording videos or taking classes in local training center. It will help us to enhance the understanding of Python and explore the unseen loopholes in your knowledge. If you don't want to do all these, join the online forum and post your answers on Python related questions.

9. Explore Libraries and Frameworks

Python consists of vast libraries and various frameworks. After getting familiar with Python's basic concepts, the next step is to explore the Python libraries. Libraries are essential to work with the domain specific projects. In the following section, we describe the brief introduction of the main libraries.

  • TensorFlow - It is an artificial intelligence library which allows us to create large scale AI based projects.
  • Django - It is an open source framework that allows us to develop web applications. It is easy, flexible, and simple to manage.
  • Flask - It is also an open source web framework. It is used to develop lightweight web applications.
  • Pandas - It is a Python library which is used to perform scientific computations.
  • Keras - It is an open source library, which is used to work around the neural network.

There are many libraries in Python. Above, we have mentioned a few of them.

10. Contribute to Open Source

As we know, Python is an open source language that means it is freely available for everyone. We can also contribute to Python online community to enhance our knowledge. Contributing to open source projects is the best way to explore own knowledge. We also receive the feedback, comments or suggestions for work that we submitted. The feedback will enable the best practices for Python programming and help us to become a good Python developer.

Usage of Python

Python is a general purpose, open source, high-level programming language and also provides number of libraries and frameworks. Python has gained popularity because of its simplicity, easy syntax and user-friendly environment. The usage of Python as follows.

In the next topic, we will discuss the Python Application, where we have defined Python's usage in detail.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Applications

Python is known for its general-purpose nature that makes it applicable in almost every domain of software development. Python makes its presence in every emerging field. It is the fastest-growing programming language and can develop any application.

Here, we are specifying application areas where Python can be applied.

Python Applications

1) Web Applications

We can use Python to develop web applications. It provides libraries to handle internet protocols such as HTML and XML, JSON, Email processing, request, beautifulSoup, Feedparser, etc. One of Python web-framework named Django is used on Instagram. Python provides many useful frameworks, and these are given below:

  • Django and Pyramid framework(Use for heavy applications)
  • Flask and Bottle (Micro-framework)
  • Plone and Django CMS (Advance Content management)

2) Desktop GUI Applications

The GUI stands for the Graphical User Interface, which provides a smooth interaction to any application. Python provides a Tk GUI library to develop a user interface. Some popular GUI libraries are given below.

  • Tkinter or Tk
  • wxWidgetM
  • Kivy (used for writing multitouch applications )
  • PyQt or Pyside

3) Console-based Application

Console-based applications run from the command-line or shell. These applications are computer program which are used commands to execute. This kind of application was more popular in the old generation of computers. Python can develop this kind of application very effectively. It is famous for having REPL, which means the Read-Eval-Print Loop that makes it the most suitable language for the command-line applications.

Python provides many free library or module which helps to build the command-line apps. The necessary IO libraries are used to read and write. It helps to parse argument and create console help text out-of-the-box. There are also advance libraries that can develop independent console apps.

4) Software Development

Python is useful for the software development process. It works as a support language and can be used to build control and management, testing, etc.

  • SCons is used to build control.
  • Buildbot and Apache Gumps are used for automated continuous compilation and testing.
  • Round or Trac for bug tracking and project management.

5) Scientific and Numeric

This is the era of Artificial intelligence where the machine can perform the task the same as the human. Python language is the most suitable language for Artificial intelligence or machine learning. It consists of many scientific and mathematical libraries, which makes easy to solve complex calculations.

Implementing machine learning algorithms require complex mathematical calculation. Python has many libraries for scientific and numeric such as Numpy, Pandas, Scipy, Scikit-learn, etc. If you have some basic knowledge of Python, you need to import libraries on the top of the code. Few popular frameworks of machine libraries are given below.

  • SciPy
  • Scikit-learn
  • NumPy
  • Pandas
  • Matplotlib

6) Business Applications

Business Applications differ from standard applications. E-commerce and ERP are an example of a business application. This kind of application requires extensively, scalability and readability, and Python provides all these features.

Oddo is an example of the all-in-one Python-based application which offers a range of business applications. Python provides a Tryton platform which is used to develop the business application.

7) Audio or Video-based Applications

Python is flexible to perform multiple tasks and can be used to create multimedia applications. Some multimedia applications which are made by using Python are TimPlayer, cplay, etc. The few multimedia libraries are given below.

  • Gstreamer
  • Pyglet
  • QT Phonon

8) 3D CAD Applications

The CAD (Computer-aided design) is used to design engineering related architecture. It is used to develop the 3D representation of a part of a system. Python can create a 3D CAD application by using the following functionalities.

  • Fandango (Popular )
  • CAMVOX
  • HeeksCNC
  • AnyCAD
  • RCAM

9) Enterprise Applications

Python can be used to create applications that can be used within an Enterprise or an Organization. Some real-time applications are OpenERP, Tryton, Picalo, etc.

10) Image Processing Application

Python contains many libraries that are used to work with the image. The image can be manipulated according to our requirements. Some libraries of image processing are given below.

  • OpenCV
  • Pillow
  • SimpleITK

In this topic, we have described all types of applications where Python plays an essential role in the development of these applications. In the next tutorial, we will learn more concepts about Python.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

How to Install Python (Environment Set-up)

In order to become Python developer, the first step is to learn how to install or update Python on a local machine or computer. In this tutorial, we will discuss the installation of Python on various operating systems.

Installation on Windows

Visit the link https://www.python.org/downloads/ to download the latest release of Python. In this process, we will install Python 3.8.6 on our Windows operating system. When we click on the above link, it will bring us the following page.

Step - 1: Select the Python's version to download.

Click on the download button.

Python Environment Set-up

Step - 2: Click on the Install Now

Double-click the executable file, which is downloaded; the following window will open. Select Customize installation and proceed. Click on the Add Path check box, it will set the Python path automatically.

Python Environment Set-up

We can also click on the customize installation to choose desired location and features. Other important thing is install launcher for the all user must be checked.

Step - 3 Installation in Process

Python Environment Set-up

Now, try to run python on the command prompt. Type the command python -version in case of python3.

Python Environment Set-up

We are ready to work with the Python.

Installation on Mac

To install python3 on MacOS, visit the link https://www.javatpoint.com/how-to-install-python-on-mac and follow the instructions given in the tutorial.


Installation on CentOS

To install Python3 on CentOS, visit the link https://www.javatpoint.com/how-to-install-python-on-centos and follow the instructions given in the tutorial.


Installation on Ubuntu

To install Python3 on Ubuntu, visit the link https://www.javatpoint.com/how-to-install-python-in-ubuntu and follow the instructions given in the tutorial.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

First Python Program

In this Section, we will discuss the basic syntax of Python, we will run a simple program to print Hello World on the console.

Python provides us the two ways to run a program:

  • Using Interactive interpreter prompt
  • Using a script file

Let's discuss each one of them in detail.

Interactive interpreter prompt

Python provides us the feature to execute the Python statement one by one at the interactive prompt. It is preferable in the case where we are concerned about the output of each line of our Python program.

To open the interactive mode, open the terminal (or command prompt) and type python (python3 in case if you have Python2 and Python3 both installed on your system).

It will open the following prompt where we can execute the Python statement and check their impact on the console.

First Python Program

After writing the print statement, press the Enter key.

First Python Program

Here, we get the message "Hello World !" printed on the console.

Using a script file (Script Mode Programming)

The interpreter prompt is best to run the single-line statements of the code. However, we cannot write the code every-time on the terminal. It is not suitable to write multiple lines of code.

Using the script mode, we can write multiple lines code into a file which can be executed later. For this purpose, we need to open an editor like notepad, create a file named and save it with .py extension, which stands for "Python". Now, we will implement the above example using the script mode.

  1. print ("hello world"); #here, we have used print() function to print the message on the console.    

To run this file named as first.py, we need to run the following command on the terminal.

First Python Program

Step - 1: Open the Python interactive shell, and click "File" then choose "New", it will open a new blank script in which we can write our code.

First Python Program

Step -2: Now, write the code and press "Ctrl+S" to save the file.

First Python Program

Step - 3: After saving the code, we can run it by clicking "Run" or "Run Module". It will display the output to the shell.

First Python Program

The output will be shown as follows.

First Python Program

Step - 4: Apart from that, we can also run the file using the operating system terminal. But, we should be aware of the path of the directory where we have saved our file.

  • Open the command line prompt and navigate to the directory.
First Python Program
  • We need to type the python keyword, followed by the file name and hit enter to run the Python file.
First Python Program

Multi-line Statements

Multi-line statements are written into the notepad like an editor and saved it with .py extension. In the following example, we have defined the execution of the multiple code lines using the Python script.

Code:

  1. name = "Andrew Venis"  
  2. branch = "Computer Science"  
  3. age = "25"  
  4. print("My name is: ", name, )  
  5. print("My age is: ", age)  

Script File:

First Python Program
First Python Program

Pros and Cons of Script Mode

The script mode has few advantages and disadvantages as well. Let's understand the following advantages of running code in script mode.

  • We can run multiple lines of code.
  • Debugging is easy in script mode.
  • It is appropriate for beginners and also for experts.

Let's see the disadvantages of the script mode.

  • We have to save the code every time if we make any change in the code.
  • It can be tedious when we run a single or a few lines of code.

Get Started with PyCharm

In our first program, we have used gedit on our CentOS as an editor. On Windows, we have an alternative like notepad or notepad++ to edit the code. However, these editors are not used as IDE for python since they are unable to show the syntax related suggestions.

JetBrains provides the most popular and a widely used cross-platform IDE PyCharm to run the python programs.

PyCharm installation

As we have already stated, PyCharm is a cross-platform IDE, and hence it can be installed on a variety of the operating systems. In this section of the tutorial, we will cover the installation process of PyCharm on Windows, MacOS, CentOS, and Ubuntu.

Windows

Installing PyCharm on Windows is very simple. To install PyCharm on Windows operating system, visit the link https://www.jetbrains.com/pycharm/download/download-thanks.html?platform=windows to download the executable installer. Double click the installer (.exe) file and install PyCharm by clicking next at each step.

To create a first program to Pycharm follows the following step.

Step - 1. Open Pycharm editor. Click on "Create New Project" option to create new project.

First Python Program

Step - 2. Select a location to save the project.

  1. We can save the newly created project at desired memory location or can keep file location as it is but atleast change the project default name untitled to "FirstProject" or something meaningful.
  2. Pycharm automatically found the installed Python interpreter.
  3. After change the name click on the "Create" Button.
First Python Program

Step - 3. Click on "File" menu and select "New". By clicking "New" option it will show various file formats. Select the "Python File".

First Python Program

Step - 4. Now type the name of the Python file and click on "OK". We have written the "FirstProgram".

First Python Program

Step - 5. Now type the first program - print("Hello World") then click on the "Run" menu to run program.

First Python Program

Step - 6. The output will appear at the bottom of the screen.

First Python Program

Basic Syntax of Python

Indentation and Comment in Python

Indentation is the most significant concept of the Python programming language. Improper use of indentation will end up "IndentationError" in our code.

Indentation is nothing but adding whitespaces before the statement when it is needed. Without indentation Python doesn't know which statement to be executed to next. Indentation also defines which statements belong to which block. If there is no indentation or improper indentation, it will display "IndentationError" and interrupt our code.

First Python Program

Python indentation defines the particular group of statements belongs to the particular block. The programming languages such as C, C++, java use the curly braces {} to define code blocks.

In Python, statements that are the same level to the right belong to the same block. We can use four whitespaces to define indentation. Let's see the following lines of code.

Example -

  1. list1 = [12345]  
  2. for i in list1:  
  3.     print(i)  
  4.     if i==4:  
  5.        break  
  6. print("End of for loop")  

Output:

1
2
3
4
End of for loop

Explanation:

In the above code, for loop has a code blocks and if the statement has its code block inside for loop. Both indented with four whitespaces. The last print() statement is not indented; that's means it doesn't belong to for loop.

Comments in Python

Comments are essential for defining the code and help us and other to understand the code. By looking the comment, we can easily understand the intention of every line that we have written in code. We can also find the error very easily, fix them, and use in other applications.

In Python, we can apply comments using the # hash character. The Python interpreter entirely ignores the lines followed by a hash character. A good programmer always uses the comments to make code under stable. Let's see the following example of a comment.

  1. name  = "Thomas"   # Assigning string value to the name variable   

We can add comment in each line of the Python code.

  1. Fees = 10000      # defining course fees is 10000  
  2. Fees = 20000      # defining course fees is 20000  

It is good idea to add code in any line of the code section of code whose purpose is not obvious. This is a best practice to learn while doing the coding.

Types of Comment

Python provides the facility to write comments in two ways- single line comment and multi-line comment.

Single-Line Comment - Single-Line comment starts with the hash # character followed by text for further explanation.

  1. # defining the marks of a student   
  2. Marks = 90  

We can also write a comment next to a code statement. Consider the following example.

  1. Name = "James"   # the name of a student is James  
  2. Marks = 90            # defining student's marks  
  3. Branch = "Computer Science"   # defining student branch  

Multi-Line Comments - Python doesn't have explicit support for multi-line comments but we can use hash # character to the multiple lines. For example -

  1. # we are defining for loop  
  2. # To iterate the given list.  
  3. # run this code.  

We can also use another way.

  1. " " "   
  2. This is an example  
  3. Of multi-line comment  
  4. Using triple-quotes   
  5. " " "  

This is the basic introduction of the comments. Visit our Python Comment tutorial to learn it in detail.

Python Identifiers

Python identifiers refer to a name used to identify a variable, function, module, class, module or other objects. There are few rules to follow while naming the Python Variable.

  • A variable name must start with either an English letter or underscore (_).
  • A variable name cannot start with the number.
  • Special characters are not allowed in the variable name.
  • The variable's name is case sensitive.

Let's understand the following example.

Example -

  1. number = 10  
  2. print(num)  
  3.   
  4. _a = 100  
  5. print(_a)  
  6.   
  7. x_y = 1000  
  8. print(x_y)  

Output:

10
100
1000

We have defined the basic syntax of the Python programming language. We must be familiar with the core concept of any programming languages. Once we memorize the concepts as mentioned above. The journey of learning Python will become easier.

CentOS

To install PyCharm on CentOS, visit the link https://www.javatpoint.com/how-to-install-pycharm-on-centos. The link will guide you to install PyCharm on the CentOS.

MacOS

To install PyCharm on MacOS, visit the link https://www.javatpoint.com/how-to-install-pycharm-on-mac. The link will guide you to install PyCharm on the MacOS.

Ubuntu

To install PyCharm on Ubuntu, visit the link https://www.javatpoint.com/how-to-install-pycharm-in-ubuntu. The link will guide you to install PyCharm on Ubuntu.


Next TopicPython Variables





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Variables

Variable is a name that is used to refer to memory location. Python variable is also known as an identifier and used to hold value.

In Python, we don't need to specify the type of variable because Python is a infer language and smart enough to get variable type.

Variable names can be a group of both the letters and digits, but they have to begin with a letter or an underscore.

It is recommended to use lowercase letters for the variable name. Rahul and rahul both are two different variables.

Identifier Naming

Variables are the example of identifiers. An Identifier is used to identify the literals used in the program. The rules to name an identifier are given below.

  • The first character of the variable must be an alphabet or underscore ( _ ).
  • All the characters except the first character may be an alphabet of lower-case(a-z), upper-case (A-Z), underscore, or digit (0-9).
  • Identifier name must not contain any white-space, or special character (!, @, #, %, ^, &, *).
  • Identifier name must not be similar to any keyword defined in the language.
  • Identifier names are case sensitive; for example, my name, and MyName is not the same.
  • Examples of valid identifiers: a123, _n, n_9, etc.
  • Examples of invalid identifiers: 1a, n%4, n 9, etc.

Declaring Variable and Assigning Values

Python does not bind us to declare a variable before using it in the application. It allows us to create a variable at the required time.

We don't need to declare explicitly variable in Python. When we assign any value to the variable, that variable is declared automatically.

The equal (=) operator is used to assign value to a variable.

Object References

It is necessary to understand how the Python interpreter works when we declare a variable. The process of treating variables is somewhat different from many other programming languages.

Python is the highly object-oriented programming language; that's why every data item belongs to a specific type of class. Consider the following example.

  1. print("John")  

Output:

John

The Python object creates an integer object and displays it to the console. In the above print statement, we have created a string object. Let's check the type of it using the Python built-in type() function.

  1. type("John")  

Output:

<class 'str'>

In Python, variables are a symbolic name that is a reference or pointer to an object. The variables are used to denote objects by that name.

Let's understand the following example

  1. a = 50   

Python Variables

In the above image, the variable a refers to an integer object.

Suppose we assign the integer value 50 to a new variable b.

a = 50

b = a


Python Variables

The variable b refers to the same object that a points to because Python does not create another object.

Let's assign the new value to b. Now both variables will refer to the different objects.

a = 50

b =100


Python Variables

Python manages memory efficiently if we assign the same variable to two different values.

Object Identity

In Python, every created object identifies uniquely in Python. Python provides the guaranteed that no two objects will have the same identifier. The built-in id() function, is used to identify the object identifier. Consider the following example.

  1. a = 50  
  2. b = a  
  3. print(id(a))  
  4. print(id(b))  
  5. # Reassigned variable a  
  6. a = 500  
  7. print(id(a))  

Output:

140734982691168
140734982691168
2822056960944

We assigned the b = a, a and b both point to the same object. When we checked by the id() function it returned the same number. We reassign a to 500; then it referred to the new object identifier.

Variable Names

We have already discussed how to declare the valid variable. Variable names can be any length can have uppercase, lowercase (A to Z, a to z), the digit (0-9), and underscore character(_). Consider the following example of valid variables names.

  1. name = "Devansh"  
  2. age = 20  
  3. marks = 80.50  
  4.   
  5. print(name)  
  6. print(age)  
  7. print(marks)  

Output:

Devansh
20
80.5

Consider the following valid variables name.

  1. name = "A"  
  2. Name = "B"  
  3. naMe = "C"  
  4. NAME = "D"  
  5. n_a_m_e = "E"  
  6. _name = "F"  
  7. name_ = "G"  
  8. _name_ = "H"  
  9. na56me = "I"  
  10.   
  11. print(name,Name,naMe,NAME,n_a_m_e, NAME, n_a_m_e, _name, name_,_name, na56me)  

Output:

A B C D E D E F G F I

In the above example, we have declared a few valid variable names such as name, _name_ , etc. But it is not recommended because when we try to read code, it may create confusion. The variable name should be descriptive to make code more readable.

The multi-word keywords can be created by the following method.

  • Camel Case - In the camel case, each word or abbreviation in the middle of begins with a capital letter. There is no intervention of whitespace. For example - nameOfStudent, valueOfVaraible, etc.
  • Pascal Case - It is the same as the Camel Case, but here the first word is also capital. For example - NameOfStudent, etc.
  • Snake Case - In the snake case, Words are separated by the underscore. For example - name_of_student, etc.

Multiple Assignment

Python allows us to assign a value to multiple variables in a single statement, which is also known as multiple assignments.

We can apply multiple assignments in two ways, either by assigning a single value to multiple variables or assigning multiple values to multiple variables. Consider the following example.

1. Assigning single value to multiple variables

Eg:

  1. x=y=z=50    
  2. print(x)    
  3. print(y)    
  4. print(z)    

Output:

50
50
50

2. Assigning multiple values to multiple variables:

Eg:

  1. a,b,c=5,10,15    
  2. print a    
  3. print b    
  4. print c    

Output:

5
10
15

The values will be assigned in the order in which variables appear.

Python Variable Types

There are two types of variables in Python - Local variable and Global variable. Let's understand the following variables.

Local Variable

Local variables are the variables that declared inside the function and have scope within the function. Let's understand the following example.

Example -

  1. # Declaring a function  
  2. def add():  
  3.     # Defining local variables. They has scope only within a function  
  4.     a = 20  
  5.     b = 30  
  6.     c = a + b  
  7.     print("The sum is:", c)  
  8.   
  9. # Calling a function  
  10. add()  

Output:

The sum is: 50

Explanation:

In the above code, we declared a function named add() and assigned a few variables within the function. These variables will be referred to as the local variables which have scope only inside the function. If we try to use them outside the function, we get a following error.

  1. add()  
  2. # Accessing local variable outside the function   
  3. print(a)  

Output:

The sum is: 50
print(a)
NameError: name 'a' is not defined

We tried to use local variable outside their scope; it threw the NameError.

Global Variables

Global variables can be used throughout the program, and its scope is in the entire program. We can use global variables inside or outside the function.

A variable declared outside the function is the global variable by default. Python provides the global keyword to use global variable inside the function. If we don't use the global keyword, the function treats it as a local variable. Let's understand the following example.

Example -

  1. # Declare a variable and initialize it  
  2. x = 101  
  3.   
  4. # Global variable in function  
  5. def mainFunction():  
  6.     # printing a global variable  
  7.     global x  
  8.     print(x)  
  9.     # modifying a global variable  
  10.     x = 'Welcome To Javatpoint'  
  11.     print(x)  
  12.   
  13. mainFunction()  
  14. print(x)  

Output:

101
Welcome To Javatpoint
Welcome To Javatpoint

Explanation:

In the above code, we declare a global variable x and assign a value to it. Next, we defined a function and accessed the declared variable using the global keyword inside the function. Now we can modify its value. Then, we assigned a new string value to the variable x.

Now, we called the function and proceeded to print x. It printed the as newly assigned value of x.

Delete a variable

We can delete the variable using the del keyword. The syntax is given below.

Syntax -

  1. del <variable_name>  

In the following example, we create a variable x and assign value to it. We deleted variable x, and print it, we get the error "variable x is not defined". The variable x will no longer use in future.

Example -

  1. # Assigning a value to x  
  2. x = 6  
  3. print(x)  
  4. # deleting a variable.   
  5. del x  
  6. print(x)  

Output:

6
Traceback (most recent call last):
File "C:/Users/DEVANSH SHARMA/PycharmProjects/Hello/multiprocessing.py", line 389, in 
print(x)
NameError: name 'x' is not defined

Maximum Possible Value of an Integer in Python

Unlike the other programming languages, Python doesn't have long int or float data types. It treats all integer values as an int data type. Here, the question arises. What is the maximum possible value can hold by the variable in Python? Consider the following example.

Example -

  1. # A Python program to display that we can store  
  2. # large numbers in Python  
  3.   
  4. a = 10000000000000000000000000000000000000000000  
  5. a = a + 1  
  6. print(type(a))  
  7. print (a)  

Output:

<class 'int'>
10000000000000000000000000000000000000000001

As we can see in the above example, we assigned a large integer value to variable x and checked its type. It printed class <int> not long int. Hence, there is no limitation number by bits and we can expand to the limit of our memory.

Python doesn't have any special data type to store larger numbers.

Print Single and Multiple Variables in Python

We can print multiple variables within the single print statement. Below are the example of single and multiple printing values.

Example - 1 (Printing Single Variable)

  1. # printing single value   
  2. a = 5  
  3. print(a)  
  4. print((a))  

Output:

5
5

Example - 2 (Printing Multiple Variables)

  1. a = 5  
  2. b = 6  
  3. # printing multiple variables  
  4. print(a,b)  
  5. # separate the variables by the comma  
  6. Print(12345678)   

Output:

5 6
1 2 3 4 5 6 7 8

Basic Fundamentals:

This section contains the fundamentals of Python, such as:

i)Tokens and their types.

ii) Comments

a)Tokens:

  • The tokens can be defined as a punctuator mark, reserved words, and each word in a statement.
  • The token is the smallest unit inside the given program.

There are following tokens in Python:

  • Keywords.
  • Identifiers.
  • Literals.
  • Operators.

We will discuss above the tokens in detail next tutorials.


Next TopicPython Data Types





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Data Types

Variables can hold values, and every value has a data-type. Python is a dynamically typed language; hence we do not need to define the type of the variable while declaring it. The interpreter implicitly binds the value with its type.

  1. a = 5  

The variable a holds integer value five and we did not define its type. Python interpreter will automatically interpret variables a as an integer type.

Python enables us to check the type of the variable used in the program. Python provides us the type() function, which returns the type of the variable passed.

Consider the following example to define the values of different data types and checking its type.

Features of Java - Javatpoint

  1. a=10  
  2. b="Hi Python"  
  3. c = 10.5  
  4. print(type(a))  
  5. print(type(b))  
  6. print(type(c))  

Output:

<type 'int'>
<type 'str'>
<type 'float'>

Standard data types

A variable can hold different types of values. For example, a person's name must be stored as a string whereas its id must be stored as an integer.

Python provides various standard data types that define the storage method on each of them. The data types defined in Python are given below.

  1. Numbers
  2. Sequence Type
  3. Boolean
  4. Set
  5. Dictionary
Python Data Types

In this section of the tutorial, we will give a brief introduction of the above data-types. We will discuss each one of them in detail later in this tutorial.

Numbers

Number stores numeric values. The integer, float, and complex values belong to a Python Numbers data-type. Python provides the type() function to know the data-type of the variable. Similarly, the isinstance() function is used to check an object belongs to a particular class.

Python creates Number objects when a number is assigned to a variable. For example;

  1. a = 5  
  2. print("The type of a", type(a))  
  3.   
  4. b = 40.5  
  5. print("The type of b", type(b))  
  6.   
  7. c = 1+3j  
  8. print("The type of c", type(c))  
  9. print(" c is a complex number", isinstance(1+3j,complex))  

Output:

The type of a <class 'int'>
The type of b <class 'float'>
The type of c <class 'complex'>
c is complex number: True

Python supports three types of numeric data.

  1. Int - Integer value can be any length such as integers 10, 2, 29, -20, -150 etc. Python has no restriction on the length of an integer. Its value belongs to int
  2. Float - Float is used to store floating-point numbers like 1.9, 9.902, 15.2, etc. It is accurate upto 15 decimal points.
  3. complex - A complex number contains an ordered pair, i.e., x + iy where x and y denote the real and imaginary parts, respectively. The complex numbers like 2.14j, 2.0 + 2.3j, etc.

Sequence Type

String

The string can be defined as the sequence of characters represented in the quotation marks. In Python, we can use single, double, or triple quotes to define a string.

String handling in Python is a straightforward task since Python provides built-in functions and operators to perform operations in the string.

In the case of string handling, the operator + is used to concatenate two strings as the operation "hello"+" python" returns "hello python".

The operator * is known as a repetition operator as the operation "Python" *2 returns 'Python Python'.

The following example illustrates the string in Python.

Example - 1

  1. str = "string using double quotes"  
  2. print(str)  
  3. s = '''''A multiline 
  4. string'''  
  5. print(s)  

Output:

string using double quotes
A multiline
string

Consider the following example of string handling.

Example - 2

  1. str1 = 'hello javatpoint' #string str1    
  2. str2 = ' how are you' #string str2    
  3. print (str1[0:2]) #printing first two character using slice operator    
  4. print (str1[4]) #printing 4th character of the string    
  5. print (str1*2#printing the string twice    
  6. print (str1 + str2) #printing the concatenation of str1 and str2    

Output:

he
o
hello javatpointhello javatpoint
hello javatpoint how are you

List

Python Lists are similar to arrays in C. However, the list can contain data of different types. The items stored in the list are separated with a comma (,) and enclosed within square brackets [].

We can use slice [:] operators to access the data of the list. The concatenation operator (+) and repetition operator (*) works with the list in the same way as they were working with the strings.

Consider the following example.

  1. list1  = [1"hi""Python"2]    
  2. #Checking type of given list  
  3. print(type(list1))  
  4.   
  5. #Printing the list1  
  6. print (list1)  
  7.   
  8. # List slicing  
  9. print (list1[3:])  
  10.   
  11. # List slicing  
  12. print (list1[0:2])   
  13.   
  14. # List Concatenation using + operator  
  15. print (list1 + list1)  
  16.   
  17. # List repetation using * operator  
  18. print (list1 * 3)  

Output:

[1, 'hi', 'Python', 2]
[2]
[1, 'hi']
[1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]
[1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2, 1, 'hi', 'Python', 2]

Tuple

A tuple is similar to the list in many ways. Like lists, tuples also contain the collection of the items of different data types. The items of the tuple are separated with a comma (,) and enclosed in parentheses ().

A tuple is a read-only data structure as we can't modify the size and value of the items of a tuple.

Let's see a simple example of the tuple.

  1. tup  = ("hi""Python"2)    
  2. # Checking type of tup  
  3. print (type(tup))    
  4.   
  5. #Printing the tuple  
  6. print (tup)  
  7.   
  8. # Tuple slicing  
  9. print (tup[1:])    
  10. print (tup[0:1])    
  11.   
  12. # Tuple concatenation using + operator  
  13. print (tup + tup)    
  14.   
  15. # Tuple repatation using * operator  
  16. print (tup * 3)     
  17.   
  18. # Adding value to tup. It will throw an error.  
  19. t[2] = "hi"  

Output:

<class 'tuple'>
('hi', 'Python', 2)
('Python', 2)
('hi',)
('hi', 'Python', 2, 'hi', 'Python', 2)
('hi', 'Python', 2, 'hi', 'Python', 2, 'hi', 'Python', 2)

Traceback (most recent call last):
File "main.py", line 14, in <module>
t[2] = "hi";
TypeError: 'tuple' object does not support item assignment

Dictionary

Dictionary is an unordered set of a key-value pair of items. It is like an associative array or a hash table where each key stores a specific value. Key can hold any primitive data type, whereas value is an arbitrary Python object.

The items in the dictionary are separated with the comma (,) and enclosed in the curly braces {}.

Consider the following example.

  1. d = {1:'Jimmy'2:'Alex'3:'john'4:'mike'}     
  2.   
  3. # Printing dictionary  
  4. print (d)  
  5.   
  6. # Accesing value using keys  
  7. print("1st name is "+d[1])   
  8. print("2nd name is "+ d[4])    
  9.   
  10. print (d.keys())    
  11. print (d.values())    

Output:

1st name is Jimmy
2nd name is mike
{1: 'Jimmy', 2: 'Alex', 3: 'john', 4: 'mike'}
dict_keys([1, 2, 3, 4])
dict_values(['Jimmy', 'Alex', 'john', 'mike'])

Boolean

Boolean type provides two built-in values, True and False. These values are used to determine the given statement true or false. It denotes by the class bool. True can be represented by any non-zero value or 'T' whereas false can be represented by the 0 or 'F'. Consider the following example.

  1. # Python program to check the boolean type  
  2. print(type(True))  
  3. print(type(False))  
  4. print(false)  

Output:

<class 'bool'>
<class 'bool'>
NameError: name 'false' is not defined

Set

Python Set is the unordered collection of the data type. It is iterable, mutable(can modify after creation), and has unique elements. In set, the order of the elements is undefined; it may return the changed sequence of the element. The set is created by using a built-in function set(), or a sequence of elements is passed in the curly braces and separated by the comma. It can contain various types of values. Consider the following example.

  1. # Creating Empty set  
  2. set1 = set()  
  3.   
  4. set2 = {'James'23,'Python'}  
  5.   
  6. #Printing Set value  
  7. print(set2)  
  8.   
  9. # Adding element to the set  
  10.   
  11. set2.add(10)  
  12. print(set2)  
  13.   
  14. #Removing element from the set  
  15. set2.remove(2)  
  16. print(set2)  

Output:

{3, 'Python', 'James', 2}
{'Python', 'James', 3, 2, 10}
{'Python', 'James', 3, 10}

Next TopicPython Keywords





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Keywords

Python Keywords are special reserved words that convey a special meaning to the compiler/interpreter. Each keyword has a special meaning and a specific operation. These keywords can't be used as a variable. Following is the List of Python Keywords.

TrueFalseNoneandas
assetdefclasscontinuebreak
elsefinallyelifdelexcept
globalforiffromimport
raisetryorreturnpass
nonlocalinnotislambda

Consider the following explanation of keywords.

  1. True - It represents the Boolean true, if the given condition is true, then it returns "True". Non-zero values are treated as true.
  2. False - It represents the Boolean false; if the given condition is false, then it returns "False". Zero value is treated as false
  3. None - It denotes the null value or void. An empty list or Zero can't be treated as None.
  4. and - It is a logical operator. It is used to check the multiple conditions. It returns true if both conditions are true. Consider the following truth table.
A B A and B
True True True
True False False
False True False
False False False

5. or - It is a logical operator in Python. It returns true if one of the conditions is true. Consider the following truth table.

A B A and B
True True True
True False True
False True True
False False False

6. not - It is a logical operator and inverts the truth value. Consider the following truth table.

A Not A
True False
False True

7. assert - This keyword is used as the debugging tool in Python. It checks the correctness of the code. It raises an AssertionError if found any error in the code and also prints the message with an error.

Example:

  1. a = 10  
  2. b = 0  
  3. print('a is dividing by Zero')  
  4. assert b != 0  
  5. print(a / b)  

Output:

a is dividing by Zero
Runtime Exception:
Traceback (most recent call last):
File "/home/40545678b342ce3b70beb1224bed345f.py", line 4, in
assert b != 0, "Divide by 0 error"
AssertionError: Divide by 0 error

8. def - This keyword is used to declare the function in Python. If followed by the function name.

  1. def my_func(a,b):  
  2.     c = a+b  
  3.     print(c)  
  4. my_func(10,20)  

Output:

30

9. class - It is used to represents the class in Python. The class is the blueprint of the objects. It is the collection of the variable and methods. Consider the following class.

  1. class Myclass:  
  2.    #Variables……..  
  3.    def function_name(self):  
  4.       #statements………  

10. continue - It is used to stop the execution of the current iteration. Consider the following example.

  1. a = 0  
  2. while a < 4:  
  3.   a += 1   
  4.   if a == 2:  
  5.     continue  
  6.   print(a)  

Output:

1
3
4

11. break - It is used to terminate the loop execution and control transfer to the end of the loop. Consider the following example.

Example

  1. for i in range(5):  
  2.     if(i==3):  
  3.         break  
  4.     print(i)  
  5. print("End of execution")  

Output:

0
1
2
End of execution

12. If - It is used to represent the conditional statement. The execution of a particular block is decided by if statement. Consider the following example.

Example

  1. i = 18  
  2. if (1 < 12):  
  3. print("I am less than 18")  

Output:

I am less than 18

13. else - The else statement is used with the if statement. When if statement returns false, then else block is executed. Consider the following example.

Example:

  1. n = 11  
  2. if(n%2 == 0):  
  3.     print("Even")  
  4. else:  
  5.     print("odd")  

Output:

Odd

14. elif - This Keyword is used to check the multiple conditions. It is short for else-if. If the previous condition is false, then check until the true condition is found. Condition the following example.

Example:

  1. marks = int(input("Enter the marks:"))  
  2. if(marks>=90):  
  3.     print("Excellent")  
  4. elif(marks<90 and marks>=75):  
  5.     print("Very Good")  
  6. elif(marks<75 and marks>=60):  
  7.     print("Good")  
  8. else:  
  9.     print("Average")  

Output:

Enter the marks:85
Very Good

15. del - It is used to delete the reference of the object. Consider the following example.

Example:

  1. a=10  
  2. b=12  
  3. del a  
  4. print(b)  
  5. # a is no longer exist  
  6. print(a)    

Output:

12
NameError: name 'a' is not defined

16. try, except - The try-except is used to handle the exceptions. The exceptions are run-time errors. Consider the following example.

Example:

  1. a = 0  
  2. try:  
  3.    b = 1/a  
  4. except Exception as e:  
  5.    print(e)  

Output:

division by zero

17. raise - The raise keyword is used to through the exception forcefully. Consider the following example.

Example

  1. a = 5  
  2. if (a>2):  
  3.    raise Exception('a should not exceed 2 ')  

Output:

Exception: a should not exceed 2

18. finally - The finally keyword is used to create a block of code that will always be executed no matter the else block raises an error or not. Consider the following example.

Example:

  1. a=0  
  2. b=5  
  3. try:  
  4.     c = b/a  
  5.     print(c)  
  6. except Exception as e:  
  7.     print(e)  
  8. finally:  
  9.     print('Finally always executed')  

Output:

division by zero
Finally always executed

19. for, while - Both keywords are used for iteration. The for keyword is used to iterate over the sequences (list, tuple, dictionary, string). A while loop is executed until the condition returns false. Consider the following example.

Example: For loop

  1. list = [1,2,3,4,5]  
  2. for i in list:  
  3.     print(i)  

Output:

1
2
3
4
5

Example: While loop

  1. a = 0  
  2. while(a<5):  
  3.     print(a)  
  4.     a = a+1  

Output:

0
1
2
3
4

20. import - The import keyword is used to import modules in the current Python script. The module contains a runnable Python code.

Example:

  1. import math  
  2. print(math.sqrt(25))  

Output:

5

21. from - This keyword is used to import the specific function or attributes in the current Python script.

Example:

  1. from math import sqrt  
  2. print(sqrt(25))  

Output:

5

22. as - It is used to create a name alias. It provides the user-define name while importing a module.

Example:

  1. import calendar as cal  
  2. print(cal.month_name[5])  

Output:

May

23. pass - The pass keyword is used to execute nothing or create a placeholder for future code. If we declare an empty class or function, it will through an error, so we use the pass keyword to declare an empty class or function.

Example:

  1. class my_class:  
  2.     pass  
  3.   
  4. def my_func():   
  5.     pass   

24. return - The return keyword is used to return the result value or none to called function.

Example:

  1. def sum(a,b):  
  2.     c = a+b  
  3.     return c  
  4.       
  5. print("The sum is:",sum(25,15))  

Output:

The sum is: 40

25. is - This keyword is used to check if the two-variable refers to the same object. It returns the true if they refer to the same object otherwise false. Consider the following example.

Example

  1. x = 5  
  2. y = 5  
  3.   
  4. a = []  
  5. b = []  
  6. print(x is y)  
  7. print(a is b)  

Output:

True
False

Note: A mutable data-types do not refer to the same object.

26. global - The global keyword is used to create a global variable inside the function. Any function can access the global. Consider the following example.

Example

  1. def my_func():  
  2.     global a   
  3.     a = 10  
  4.     b = 20  
  5.     c = a+b  
  6.     print(c)  
  7.       
  8. my_func()  
  9.   
  10. def func():  
  11.     print(a)  
  12.       
  13. func()  

Output:

30
10

27. nonlocal - The nonlocal is similar to the global and used to work with a variable inside the nested function(function inside a function). Consider the following example.

Example

  1. def outside_function():    
  2.     a = 20     
  3.     def inside_function():    
  4.         nonlocal a    
  5.         a = 30    
  6.         print("Inner function: ",a)    
  7.     inside_function()    
  8.     print("Outer function: ",a)    
  9. outside_function()   

Output:

Inner function: 30
Outer function: 30

28. lambda - The lambda keyword is used to create the anonymous function in Python. It is an inline function without a name. Consider the following example.

Example

  1. a = lambda x: x**2  
  2. for i in range(1,6):  
  3.   print(a(i))  

Output:

1
4
9
16
25

29. yield - The yield keyword is used with the Python generator. It stops the function's execution and returns value to the caller. Consider the following example.

Example

  1. def fun_Generator():  
  2.   yield 1  
  3.   yield 2  
  4.   yield 3  
  5.   
  6.   
  7. # Driver code to check above generator function   
  8. for value in fun_Generator():  
  9.   print(value)  

Output:

1
2
3

30. with - The with keyword is used in the exception handling. It makes code cleaner and more readable. The advantage of using with, we don't need to call close(). Consider the following example.

Example

  1. with open('file_path''w') as file:   
  2.     file.write('hello world !')  

31. None - The None keyword is used to define the null value. It is remembered that None does not indicate 0, false, or any empty data-types. It is an object of its data type, which is Consider the following example.

Example:

  1. def return_none():  
  2.   a = 10  
  3.   b = 20  
  4.   c = a + b  
  5.   
  6. x = return_none()  
  7. print(x)  

Output:

None

We have covered all Python keywords. This is the brief introduction of Python Keywords. We will learn more in the upcoming tutorials.


Next TopicPython Literals





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Literals

Python Literals can be defined as data that is given in a variable or constant.

Python supports the following literals:

1. String literals:

String literals can be formed by enclosing a text in the quotes. We can use both single as well as double quotes to create a string.

Example:

  1. "Aman" , '12345'  

Types of Strings:

There are two types of Strings supported in Python:

a) Single-line String- Strings that are terminated within a single-line are known as Single line Strings.

Example:

  1. text1='hello'  

b) Multi-line String - A piece of text that is written in multiple lines is known as multiple lines string.

There are two ways to create multiline strings:

1) Adding black slash at the end of each line.

Example:

  1. text1='hello\    
  2. user'    
  3. print(text1)  
'hellouser'

2) Using triple quotation marks:-

Example:

  1. str2='''''welcome  
  2. to  
  3. SSSIT'''    
  4. print str2   

Output:

welcome
to
SSSIT

II. Numeric literals:

Numeric Literals are immutable. Numeric literals can belong to following four different numerical types.

Int(signed integers)Long(long integers)float(floating point)Complex(complex)
Numbers( can be both positive and negative) with no fractional part.eg: 100Integers of unlimited size followed by lowercase or uppercase L eg: 87032845LReal numbers with both integer and fractional part eg: -26.2In the form of a+bj where a forms the real part and b forms the imaginary part of the complex number. eg: 3.14j

Example - Numeric Literals

  1. x = 0b10100 #Binary Literals  
  2. y = 100 #Decimal Literal   
  3. z = 0o215 #Octal Literal  
  4. u = 0x12d #Hexadecimal Literal  
  5.   
  6. #Float Literal  
  7. float_1 = 100.5   
  8. float_2 = 1.5e2  
  9.   
  10. #Complex Literal   
  11. a = 5+3.14j  
  12.   
  13. print(x, y, z, u)  
  14. print(float_1, float_2)  
  15. print(a, a.imag, a.real)  

Output:

20 100 141 301
100.5 150.0
(5+3.14j) 3.14 5.0

III. Boolean literals:

A Boolean literal can have any of the two values: True or False.

Example - Boolean Literals

  1. x = (1 == True)  
  2. y = (2 == False)  
  3. z = (3 == True)  
  4. a = True + 10  
  5. b = False + 10  
  6.   
  7. print("x is", x)  
  8. print("y is", y)  
  9. print("z is", z)  
  10. print("a:", a)  
  11. print("b:", b)  

Output:

x is True
y is False
z is False
a: 11
b: 10

IV. Special literals.

Python contains one special literal i.e., None.

None is used to specify to that field that is not created. It is also used for the end of lists in Python.

Example - Special Literals

  1. val1=10    
  2. val2=None    
  3. print(val1)     
  4. print(val2)  

Output:

10
None

V. Literal Collections.

Python provides the four types of literal collection such as List literals, Tuple literals, Dict literals, and Set literals.

List:

  • List contains items of different data types. Lists are mutable i.e., modifiable.
  • The values stored in List are separated by comma(,) and enclosed within square brackets([]). We can store different types of data in a List.

Example - List literals

  1. list=['John',678,20.4,'Peter']    
  2. list1=[456,'Andrew']    
  3. print(list)    
  4. print(list + list1)  

Output:

['John', 678, 20.4, 'Peter']
['John', 678, 20.4, 'Peter', 456, 'Andrew']

Dictionary:

  • Python dictionary stores the data in the key-value pair.
  • It is enclosed by curly-braces {} and each pair is separated by the commas(,).

Example

  1. dict = {'name''Pater''Age':18,'Roll_nu':101}  
  2. print(dict)  

Output:

{'name': 'Pater', 'Age': 18, 'Roll_nu': 101}

Tuple:

  • Python tuple is a collection of different data-type. It is immutable which means it cannot be modified after creation.
  • It is enclosed by the parentheses () and each element is separated by the comma(,).

Example

  1. tup = (10,20,"Dev",[2,3,4])  
  2. print(tup)  

Output:

(10, 20, 'Dev', [2, 3, 4])

Set:

  • Python set is the collection of the unordered dataset.
  • It is enclosed by the {} and each element is separated by the comma(,).

Example: - Set Literals

  1. set = {'apple','grapes','guava','papaya'}  
  2. print(set)  

Output:

{'guava', 'apple', 'papaya', 'grapes'}

Next TopicPython Operators





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Operators

The operator can be defined as a symbol which is responsible for a particular operation between two operands. Operators are the pillars of a program on which the logic is built in a specific programming language. Python provides a variety of operators, which are described as follows.

  • Arithmetic operators
  • Comparison operators
  • Assignment Operators
  • Logical Operators
  • Bitwise Operators
  • Membership Operators
  • Identity Operators

Arithmetic Operators

Arithmetic operators are used to perform arithmetic operations between two operands. It includes + (addition), - (subtraction), *(multiplication), /(divide), %(reminder), //(floor division), and exponent (**) operators.

Consider the following table for a detailed explanation of arithmetic operators.

Operator Description
+ (Addition) It is used to add two operands. For example, if a = 20, b = 10 => a+b = 30
- (Subtraction) It is used to subtract the second operand from the first operand. If the first operand is less than the second operand, the value results negative. For example, if a = 20, b = 10 => a - b = 10
/ (divide) It returns the quotient after dividing the first operand by the second operand. For example, if a = 20, b = 10 => a/b = 2.0
* (Multiplication) It is used to multiply one operand with the other. For example, if a = 20, b = 10 => a * b = 200
% (reminder) It returns the reminder after dividing the first operand by the second operand. For example, if a = 20, b = 10 => a%b = 0
** (Exponent) It is an exponent operator represented as it calculates the first operand power to the second operand.
// (Floor division) It gives the floor value of the quotient produced by dividing the two operands.

Comparison operator

Comparison operators are used to comparing the value of the two operands and returns Boolean true or false accordingly. The comparison operators are described in the following table.

Operator Description
== If the value of two operands is equal, then the condition becomes true.
!= If the value of two operands is not equal, then the condition becomes true.
<= If the first operand is less than or equal to the second operand, then the condition becomes true.
>= If the first operand is greater than or equal to the second operand, then the condition becomes true.
> If the first operand is greater than the second operand, then the condition becomes true.
< If the first operand is less than the second operand, then the condition becomes true.

Assignment Operators

The assignment operators are used to assign the value of the right expression to the left operand. The assignment operators are described in the following table.

Operator Description
= It assigns the value of the right expression to the left operand.
+= It increases the value of the left operand by the value of the right operand and assigns the modified value back to left operand. For example, if a = 10, b = 20 => a+ = b will be equal to a = a+ b and therefore, a = 30.
-= It decreases the value of the left operand by the value of the right operand and assigns the modified value back to left operand. For example, if a = 20, b = 10 => a- = b will be equal to a = a- b and therefore, a = 10.
*= It multiplies the value of the left operand by the value of the right operand and assigns the modified value back to then the left operand. For example, if a = 10, b = 20 => a* = b will be equal to a = a* b and therefore, a = 200.
%= It divides the value of the left operand by the value of the right operand and assigns the reminder back to the left operand. For example, if a = 20, b = 10 => a % = b will be equal to a = a % b and therefore, a = 0.
**= a**=b will be equal to a=a**b, for example, if a = 4, b =2, a**=b will assign 4**2 = 16 to a.
//= A//=b will be equal to a = a// b, for example, if a = 4, b = 3, a//=b will assign 4//3 = 1 to a.

Bitwise Operators

The bitwise operators perform bit by bit operation on the values of the two operands. Consider the following example.

For example,

  1. if a = 7   
  2.    b = 6     
  3. then, binary (a) = 0111    
  4.     binary (b) = 0110    
  5.     
  6. hence, a & b = 0011    
  7.       a | b = 0111    
  8.              a ^ b = 0100    
  9.        ~ a = 1000  

Operator Description
& (binary and) If both the bits at the same place in two operands are 1, then 1 is copied to the result. Otherwise, 0 is copied.
| (binary or) The resulting bit will be 0 if both the bits are zero; otherwise, the resulting bit will be 1.
^ (binary xor) The resulting bit will be 1 if both the bits are different; otherwise, the resulting bit will be 0.
~ (negation) It calculates the negation of each bit of the operand, i.e., if the bit is 0, the resulting bit will be 1 and vice versa.
<< (left shift) The left operand value is moved left by the number of bits present in the right operand.
>> (right shift) The left operand is moved right by the number of bits present in the right operand.

Logical Operators

The logical operators are used primarily in the expression evaluation to make a decision. Python supports the following logical operators.

Operator Description
and If both the expression are true, then the condition will be true. If a and b are the two expressions, a → true, b → true => a and b → true.
or If one of the expressions is true, then the condition will be true. If a and b are the two expressions, a → true, b → false => a or b → true.
not If an expression a is true, then not (a) will be false and vice versa.

Membership Operators

Python membership operators are used to check the membership of value inside a Python data structure. If the value is present in the data structure, then the resulting value is true otherwise it returns false.

Operator Description
in It is evaluated to be true if the first operand is found in the second operand (list, tuple, or dictionary).
not in It is evaluated to be true if the first operand is not found in the second operand (list, tuple, or dictionary).

Identity Operators

The identity operators are used to decide whether an element certain class or type.

Operator Description
is It is evaluated to be true if the reference present at both sides point to the same object.
is not It is evaluated to be true if the reference present at both sides do not point to the same object.

Operator Precedence

The precedence of the operators is essential to find out since it enables us to know which operator should be evaluated first. The precedence table of the operators in Python is given below.

Operator Description
** The exponent operator is given priority over all the others used in the expression.
~ + - The negation, unary plus, and minus.
* / % // The multiplication, divide, modules, reminder, and floor division.
+ - Binary plus, and minus
>> << Left shift. and right shift
& Binary and.
^ | Binary xor, and or
<= < > >= Comparison operators (less than, less than equal to, greater than, greater then equal to).
<> == != Equality operators.
= %= /= //= -= +=
*= **=
Assignment operators
is is not Identity operators
in not in Membership operators
not or and Logical operators

Next TopicPython Comments





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Comments

Python Comment is an essential tool for the programmers. Comments are generally used to explain the code. We can easily understand the code if it has a proper explanation. A good programmer must use the comments because in the future anyone wants to modify the code as well as implement the new module; then, it can be done easily.

In the other programming language such as C++, It provides the // for single-lined comment and /*.... */ for multiple-lined comment, but Python provides the single-lined Python comment. To apply the comment in the code we use the hash(#) at the beginning of the statement or code.

Let's understand the following example.

  1. # This is the print statement  
  2. print("Hello Python")  

Here we have written comment over the print statement using the hash(#). It will not affect our print statement.

Multiline Python Comment

We must use the hash(#) at the beginning of every line of code to apply the multiline Python comment. Consider the following example.

  1. # First line of the comment   
  2. # Second line of the comment  
  3. # Third line of the comment  

Example:

  1. # Variable a holds value 5  
  2. # Variable b holds value 10  
  3. # Variable c holds sum of a and b  
  4. # Print the result  
  5. a = 5  
  6. b = 10  
  7. c = a+b  
  8. print("The sum is:", c)  

Output:

The sum is: 15

The above code is very readable even the absolute beginners can under that what is happening in each line of the code. This is the advantage of using comments in code.

We can also use the triple quotes ('''''') for multiline comment. The triple quotes are also used to string formatting. Consider the following example.

Docstrings Python Comment

The docstring comment is mostly used in the module, function, class or method. It is a documentation Python string. We will explain the class/method in further tutorials.

Example:

  1. def intro():  
  2.   """ 
  3.   This function prints Hello Joseph 
  4.   """  
  5.   print("Hi Joseph")              
  6. intro()  

Output:

Hello Joseph

We can check a function's docstring by using the __doc__ attribute.

Generally, four whitespaces are used as the indentation. The amount of indentation depends on user, but it must be consistent throughout that block.
  1. def intro():  
  2.   """ 
  3.   This function prints Hello Joseph 
  4.   """  
  5.   print("Hello Joseph")              
  6. intro.__doc__  

Output:

Output:
'\n This function prints Hello Joseph\n '

Note: The docstring must be the first thing in the function; otherwise, Python interpreter cannot get the docstring.

Python indentation

Python indentation uses to define the block of the code. The other programming languages such as C, C++, and Java use curly braces {}, whereas Python uses an indentation. Whitespaces are used as indentation in Python.

Indentation uses at the beginning of the code and ends with the unintended line. That same line indentation defines the block of the code (body of a function, loop, etc.)

Generally, four whitespaces are used as the indentation. The amount of indentation depends on user, but it must be consistent throughout that block.

  1. for i in range(5):  
  2.     print(i)  
  3.     if(i == 3):  
  4.         break  

To indicate a block of code we indented each line of the block by the same whitespaces.

Consider the following example.

  1. dn = int(input("Enter the number:"))  
  2. if(n%2 == 0):  
  3.     print("Even Number")  
  4. else:  
  5.     print("Odd Number")  
  6.      
  7. print("Task Complete")  

Output:

Enter the number: 10
Even Number
Task Complete

The above code, if and else are two separate code blocks. Both code blocks are indented four spaces. The print("Task Complete") statement is not indented four whitespaces and it is out of the if-else block.

If the indentation is not used properly, then that will result in IndentationError.


Next TopicPython If Else





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python If-else statements

Decision making is the most important aspect of almost all the programming languages. As the name implies, decision making allows us to run a particular block of code for a particular decision. Here, the decisions are made on the validity of the particular conditions. Condition checking is the backbone of decision making.

In python, decision making is performed by the following statements.

Statement Description
If Statement The if statement is used to test a specific condition. If the condition is true, a block of code (if-block) will be executed.
If - else Statement The if-else statement is similar to if statement except the fact that, it also provides the block of the code for the false case of the condition to be checked. If the condition provided in the if statement is false, then the else statement will be executed.
Nested if Statement Nested if statements enable us to use if ? else statement inside an outer if statement.

Indentation in Python

For the ease of programming and to achieve simplicity, python doesn't allow the use of parentheses for the block level code. In Python, indentation is used to declare a block. If two statements are at the same indentation level, then they are the part of the same block.

Generally, four spaces are given to indent the statements which are a typical amount of indentation in python.

Indentation is the most used part of the python language since it declares the block of code. All the statements of one block are intended at the same level indentation. We will see how the actual indentation takes place in decision making and other stuff in python.

The if statement

The if statement is used to test a particular condition and if the condition is true, it executes a block of code known as if-block. The condition of if statement can be any valid logical expression which can be either evaluated to true or false.

Python If-else statements

The syntax of the if-statement is given below.

  1. if expression:  
  2.     statement  

Example 1

  1. num = int(input("enter the number?"))  
  2. if num%2 == 0:  
  3.     print("Number is even")  

Output:

enter the number?10
Number is even

Example 2 : Program to print the largest of the three numbers.

  1. a = int(input("Enter a? "));  
  2. b = int(input("Enter b? "));  
  3. c = int(input("Enter c? "));  
  4. if a>b and a>c:  
  5.     print("a is largest");  
  6. if b>a and b>c:  
  7.     print("b is largest");  
  8. if c>a and c>b:  
  9.     print("c is largest");  

Output:

Enter a? 100
Enter b? 120
Enter c? 130
c is largest

The if-else statement

The if-else statement provides an else block combined with the if statement which is executed in the false case of the condition.

If the condition is true, then the if-block is executed. Otherwise, the else-block is executed.

Python If-else statements

The syntax of the if-else statement is given below.

  1. if condition:  
  2.     #block of statements   
  3. else:   
  4.     #another block of statements (else-block)   

Example 1 : Program to check whether a person is eligible to vote or not.

  1. age = int (input("Enter your age? "))  
  2. if age>=18:  
  3.     print("You are eligible to vote !!");  
  4. else:  
  5.     print("Sorry! you have to wait !!");  

Output:

Enter your age? 90
You are eligible to vote !!

Example 2: Program to check whether a number is even or not.

  1. num = int(input("enter the number?"))  
  2. if num%2 == 0:  
  3.     print("Number is even...")  
  4. else:  
  5.     print("Number is odd...")  

Output:

enter the number?10
Number is even

The elif statement

The elif statement enables us to check multiple conditions and execute the specific block of statements depending upon the true condition among them. We can have any number of elif statements in our program depending upon our need. However, using elif is optional.

The elif statement works like an if-else-if ladder statement in C. It must be succeeded by an if statement.

The syntax of the elif statement is given below.

  1. if expression 1:   
  2.     # block of statements   
  3.   
  4. elif expression 2:   
  5.     # block of statements   
  6.   
  7. elif expression 3:   
  8.     # block of statements   
  9.   
  10. else:   
  11.     # block of statements  
Python If-else statements

Example 1

  1. number = int(input("Enter the number?"))  
  2. if number==10:  
  3.     print("number is equals to 10")  
  4. elif number==50:  
  5.     print("number is equal to 50");  
  6. elif number==100:  
  7.     print("number is equal to 100");  
  8. else:  
  9.     print("number is not equal to 10, 50 or 100");  

Output:

Enter the number?15
number is not equal to 10, 50 or 100

Example 2

  1. marks = int(input("Enter the marks? "))  
  2. f marks > 85 and marks <= 100:  
  3.    print("Congrats ! you scored grade A ...")  
  4. lif marks > 60 and marks <= 85:  
  5.    print("You scored grade B + ...")  
  6. lif marks > 40 and marks <= 60:  
  7.    print("You scored grade B ...")  
  8. lif (marks > 30 and marks <= 40):  
  9.    print("You scored grade C ...")  
  10. lse:  
  11.    print("Sorry you are fail ?")  
Next TopicPython Loops





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Loops

The flow of the programs written in any programming language is sequential by default. Sometimes we may need to alter the flow of the program. The execution of a specific code may need to be repeated several numbers of times.

For this purpose, The programming languages provide various types of loops which are capable of repeating some specific code several numbers of times. Consider the following diagram to understand the working of a loop statement.

Python Loops

Why we use loops in python?

The looping simplifies the complex problems into the easy ones. It enables us to alter the flow of the program so that instead of writing the same code again and again, we can repeat the same code for a finite number of times. For example, if we need to print the first 10 natural numbers then, instead of using the print statement 10 times, we can print inside a loop which runs up to 10 iterations.

Advantages of loops

There are the following advantages of loops in Python.

  1. It provides code re-usability.
  2. Using loops, we do not need to write the same code again and again.
  3. Using loops, we can traverse over the elements of data structures (array or linked lists).

There are the following loop statements in Python.

Loop Statement Description
for loop The for loop is used in the case where we need to execute some part of the code until the given condition is satisfied. The for loop is also called as a per-tested loop. It is better to use for loop if the number of iteration is known in advance.
while loop The while loop is to be used in the scenario where we don't know the number of iterations in advance. The block of statements is executed in the while loop until the condition specified in the while loop is satisfied. It is also called a pre-tested loop.
do-while loop The do-while loop continues until a given condition satisfies. It is also called post tested loop. It is used when it is necessary to execute the loop at least once (mostly menu driven programs).
Next TopicPython For Loop





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python for loop

The for loop in Python is used to iterate the statements or a part of the program several times. It is frequently used to traverse the data structures like list, tuple, or dictionary.

The syntax of for loop in python is given below.

  1. for iterating_var in sequence:    
  2.     statement(s)    

The for loop flowchart

Python for loop

For loop Using Sequence

Example-1: Iterating string using for loop

  1. str = "Python"  
  2. for i in str:  
  3.     print(i)  

Output:

P
y
t
h
o
n

Example- 2: Program to print the table of the given number .

  1. list = [1,2,3,4,5,6,7,8,9,10]  
  2. n = 5  
  3. for i in list:  
  4.     c = n*i  
  5.     print(c)  

Output:

5
10
15
20
25
30
35
40
45
50s

Example-4: Program to print the sum of the given list.

  1. list = [10,30,23,43,65,12]  
  2. sum = 0  
  3. for i in list:  
  4.     sum = sum+i  
  5. print("The sum is:",sum)  

Output:

The sum is: 183

For loop Using range() function

The range() function

The range() function is used to generate the sequence of the numbers. If we pass the range(10), it will generate the numbers from 0 to 9. The syntax of the range() function is given below.

Syntax:

  1. range(start,stop,step size)  
  • The start represents the beginning of the iteration.
  • The stop represents that the loop will iterate till stop-1. The range(1,5) will generate numbers 1 to 4 iterations. It is optional.
  • The step size is used to skip the specific numbers from the iteration. It is optional to use. By default, the step size is 1. It is optional.

Consider the following examples:

Example-1: Program to print numbers in sequence.

  1. for i in range(10):  
  2.     print(i,end = ' ')  

Output:

0 1 2 3 4 5 6 7 8 9

Example - 2: Program to print table of given number.

  1. n = int(input("Enter the number "))  
  2. for i in range(1,11):  
  3.     c = n*i  
  4.     print(n,"*",i,"=",c)  

Output:

Enter the number 10
10 * 1 = 10
10 * 2 = 20
10 * 3 = 30
10 * 4 = 40
10 * 5 = 50
10 * 6 = 60
10 * 7 = 70
10 * 8 = 80
10 * 9 = 90
10 * 10 = 100

Example-3: Program to print even number using step size in range().

  1. n = int(input("Enter the number "))  
  2. for i in range(2,n,2):  
  3.     print(i)  

Output:

Enter the number 20
2
4
6
8
10
12
14
16
18

We can also use the range() function with sequence of numbers. The len() function is combined with range() function which iterate through a sequence using indexing. Consider the following example.

  1. list = ['Peter','Joseph','Ricky','Devansh']  
  2. for i in range(len(list)):  
  3.     print("Hello",list[i])  

Output:

Hello Peter
Hello Joseph
Hello Ricky
Hello Devansh

Nested for loop in python

Python allows us to nest any number of for loops inside a for loop. The inner loop is executed n number of times for every iteration of the outer loop. The syntax is given below.

Syntax

  1. for iterating_var1 in sequence:  #outer loop  
  2.     for iterating_var2 in sequence:  #inner loop  
  3.         #block of statements     
  4. #Other statements    

Example- 1: Nested for loop

  1. # User input for number of rows  
  2. rows = int(input("Enter the rows:"))  
  3. # Outer loop will print number of rows  
  4. for i in range(0,rows+1):  
  5. # Inner loop will print number of Astrisk  
  6.     for j in range(i):  
  7.         print("*",end = '')  
  8.     print()  

Output:

Enter the rows:5
*
**
***
****
*****

Example-2: Program to number pyramid.

  1. rows = int(input("Enter the rows"))  
  2. for i in range(0,rows+1):  
  3.     for j in range(i):  
  4.         print(i,end = '')  
  5.     print()  

Output:

1
22
333
4444
55555

Using else statement with for loop

Unlike other languages like C, C++, or Java, Python allows us to use the else statement with the for loop which can be executed only when all the iterations are exhausted. Here, we must notice that if the loop contains any of the break statement then the else statement will not be executed.

Example 1

  1. for i in range(0,5):    
  2.     print(i)    
  3. else:  
  4.     print("for loop completely exhausted, since there is no break.")  

Output:

0
1
2
3
4
for loop completely exhausted, since there is no break.

The for loop completely exhausted, since there is no break.

Example 2

  1. for i in range(0,5):    
  2.     print(i)    
  3.     break;    
  4. else:print("for loop is exhausted");    
  5. print("The loop is broken due to break statement...came out of the loop")    

In the above example, the loop is broken due to the break statement; therefore, the else statement will not be executed. The statement present immediate next to else block will be executed.

Output:

0

The loop is broken due to the break statement...came out of the loop. We will learn more about the break statement in next tutorial.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python While loop

The Python while loop allows a part of the code to be executed until the given condition returns false. It is also known as a pre-tested loop.

It can be viewed as a repeating if statement. When we don't know the number of iterations then the while loop is most effective to use.

The syntax is given below.

  1. while expression:    
  2.     statements    

Here, the statements can be a single statement or a group of statements. The expression should be any valid Python expression resulting in true or false. The true is any non-zero value and false is 0.

While loop Flowchart

Python While loop

Loop Control Statements

We can change the normal sequence of while loop's execution using the loop control statement. When the while loop's execution is completed, all automatic objects defined in that scope are demolished. Python offers the following control statement to use within the while loop.

1. Continue Statement - When the continue statement is encountered, the control transfer to the beginning of the loop. Let's understand the following example.

Example:

  1. # prints all letters except 'a' and 't'   
  2. i = 0  
  3. str1 = 'javatpoint'  
  4.   
  5. while i < len(str1):   
  6.     if str1[i] == 'a' or str1[i] == 't':   
  7.         i += 1  
  8.         continue  
  9.     print('Current Letter :', a[i])   
  10.     i += 1  

Output:

Current Letter : j
Current Letter : v
Current Letter : p
Current Letter : o
Current Letter : i
Current Letter : n

2. Break Statement - When the break statement is encountered, it brings control out of the loop.

Example:

  1. # The control transfer is transfered  
  2. # when break statement soon it sees t  
  3. i = 0  
  4. str1 = 'javatpoint'  
  5.   
  6. while i < len(str1):   
  7.     if str1[i] == 't':   
  8.         i += 1  
  9.         break  
  10.     print('Current Letter :', str1[i])   
  11.     i += 1  

Output:

Current Letter : j
Current Letter : a
Current Letter : v
Current Letter : a

3. Pass Statement - The pass statement is used to declare the empty loop. It is also used to define empty class, function, and control statement. Let's understand the following example.

Example -

  1. # An empty loop   
  2. str1 = 'javatpoint'  
  3. i = 0  
  4.   
  5. while i < len(str1):   
  6.     i += 1  
  7.     pass  
  8. print('Value of i :', i)  

Output:

Value of i : 10

Example-1: Program to print 1 to 10 using while loop

  1. i=1  
  2. #The while loop will iterate until condition becomes false.  
  3. While(i<=10):    
  4.     print(i)   
  5.     i=i+1   

Output:

1
2
3
4
5
6
7
8
9
10

Example -2: Program to print table of given numbers.

  1. i=1    
  2. number=0    
  3. b=9    
  4. number = int(input("Enter the number:"))    
  5. while i<=10:    
  6.     print("%d X %d = %d \n"%(number,i,number*i))    
  7.     i = i+1    

Output:

Enter the number:10
10 X 1 = 10

10 X 2 = 20

10 X 3 = 30

10 X 4 = 40

10 X 5 = 50

10 X 6 = 60

10 X 7 = 70

10 X 8 = 80

10 X 9 = 90

10 X 10 = 100

Infinite while loop

If the condition is given in the while loop never becomes false, then the while loop will never terminate, and it turns into the infinite while loop.

Any non-zero value in the while loop indicates an always-true condition, whereas zero indicates the always-false condition. This type of approach is useful if we want our program to run continuously in the loop without any disturbance.

Example 1

  1. while (1):    
  2.     print("Hi! we are inside the infinite while loop")  

Output:

Hi! we are inside the infinite while loop
Hi! we are inside the infinite while loop

Example 2

  1. var = 1    
  2. while(var != 2):    
  3.     i = int(input("Enter the number:"))    
  4.     print("Entered value is %d"%(i))    

Output:

Enter the number:10
Entered value is 10
Enter the number:10
Entered value is 10
Enter the number:10
Entered value is 10
Infinite time

Using else with while loop

Python allows us to use the else statement with the while loop also. The else block is executed when the condition given in the while statement becomes false. Like for loop, if the while loop is broken using break statement, then the else block will not be executed, and the statement present after else block will be executed. The else statement is optional to use with the while loop. Consider the following example.

Example 1

  1. i=1   
  2. while(i<=5):    
  3.     print(i)    
  4.     i=i+1    
  5. else:  
  6.     print("The while loop exhausted")    

Example 2

  1. i=1    
  2. while(i<=5):    
  3.     print(i)    
  4.     i=i+1    
  5.     if(i==3):    
  6.         break   
  7. else:  
  8.     print("The while loop exhausted")  

Output:

1
2

In the above code, when the break statement encountered, then while loop stopped its execution and skipped the else statement.

Example-3 Program to print Fibonacci numbers to given limit

  1. terms = int(input("Enter the terms "))  
  2. # first two intial terms  
  3. a = 0  
  4. b = 1  
  5. count = 0  
  6.   
  7. # check if the number of terms is Zero or negative  
  8. if (terms <= 0):  
  9.    print("Please enter a valid integer")  
  10. elif (terms == 1):  
  11.    print("Fibonacci sequence upto",limit,":")  
  12.    print(a)  
  13. else:  
  14.    print("Fibonacci sequence:")  
  15.    while (count < terms) :  
  16.        print(a, end = ' ')  
  17.        c = a + b  
  18.        # updateing values  
  19.        a = b  
  20.        b = c  
  21.      
  22.     count += 1  

Output:

Enter the terms 10
Fibonacci sequence:
0 1 1 2 3 5 8 13 21 34






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python break statement

The break is a keyword in python which is used to bring the program control out of the loop. The break statement breaks the loops one by one, i.e., in the case of nested loops, it breaks the inner loop first and then proceeds to outer loops. In other words, we can say that break is used to abort the current execution of the program and the control goes to the next line after the loop.

The break is commonly used in the cases where we need to break the loop for a given condition.

The syntax of the break is given below.

  1. #loop statements  
  2. break;   

Example 1

  1. list =[1,2,3,4]  
  2. count = 1;  
  3. for i in list:  
  4.     if i == 4:  
  5.         print("item matched")  
  6.         count = count + 1;  
  7.         break  
  8. print("found at",count,"location");  

Output:

item matched
found at 2 location

Example 2

  1. str = "python"  
  2. for i in str:  
  3.     if i == 'o':  
  4.         break  
  5.     print(i);  

Output:

p
y
t
h

Example 3: break statement with while loop

  1. i = 0;  
  2. while 1:  
  3.     print(i," ",end=""),  
  4.     i=i+1;  
  5.     if i == 10:  
  6.         break;  
  7. print("came out of while loop");  

Output:

0 1 2 3 4 5 6 7 8 9 came out of while loop

Example 3

  1. n=2  
  2. while 1:  
  3.     i=1;  
  4.     while i<=10:  
  5.         print("%d X %d = %d\n"%(n,i,n*i));  
  6.         i = i+1;  
  7.     choice = int(input("Do you want to continue printing the table, press 0 for no?"))  
  8.     if choice == 0:  
  9.         break;      
  10.     n=n+1  

Output:

2 X 1 = 2

2 X 2 = 4

2 X 3 = 6

2 X 4 = 8

2 X 5 = 10

2 X 6 = 12

2 X 7 = 14

2 X 8 = 16

2 X 9 = 18

2 X 10 = 20

Do you want to continue printing the table, press 0 for no?1

3 X 1 = 3

3 X 2 = 6

3 X 3 = 9

3 X 4 = 12

3 X 5 = 15

3 X 6 = 18

3 X 7 = 21

3 X 8 = 24

3 X 9 = 27

3 X 10 = 30

Do you want to continue printing the table, press 0 for no?0





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python continue Statement

The continue statement in Python is used to bring the program control to the beginning of the loop. The continue statement skips the remaining lines of code inside the loop and start with the next iteration. It is mainly used for a particular condition inside the loop so that we can skip some specific code for a particular condition.The continue statement in Python is used to bring the program control to the beginning of the loop. The continue statement skips the remaining lines of code inside the loop and start with the next iteration. It is mainly used for a particular condition inside the loop so that we can skip some specific code for a particular condition.

Syntax

  1. #loop statements    
  2. continue  
  3. #the code to be skipped     

Flow Diagram

Python continue Statement

Consider the following examples.

Example 1

  1. i = 0                     
  2. while(i < 10):                
  3.    i = i+1  
  4.    if(i == 5):  
  5.       continue  
  6.    print(i)  

Output:

1
2
3
4
6
7
8
9
10

Observe the output of above code, the value 5 is skipped because we have provided the if condition using with continue statement in while loop. When it matched with the given condition then control transferred to the beginning of the while loop and it skipped the value 5 from the code.

Let's have a look at another example:

Example 2

  1. str = "JavaTpoint"  
  2. for i in str:  
  3.     if(i == 'T'):  
  4.         continue  
  5.     print(i)  

Output:

J
a
v
a
p
o
i
n
t

Pass Statement

The pass statement is a null operation since nothing happens when it is executed. It is used in the cases where a statement is syntactically needed but we don't want to use any executable statement at its place.

For example, it can be used while overriding a parent class method in the subclass but don't want to give its specific implementation in the subclass.

Pass is also used where the code will be written somewhere but not yet written in the program file. Consider the following example.

Example

  1. list = [1,2,3,4,5]    
  2. flag = 0    
  3. for i in list:    
  4.     print("Current element:",i,end=" ");    
  5.     if i==3:    
  6.         pass    
  7.         print("\nWe are inside pass block\n");    
  8.         flag = 1    
  9.     if flag==1:    
  10.         print("\nCame out of pass\n");    
  11.         flag=0   

Output:

Current element: 1 Current element: 2 Current element: 3
We are inside pass block


Came out of pass

Current element: 4 Current element: 5

We will learn more about the pass statement in the next tutorial.


Next TopicPython Pass





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Pass

In Python, the pass keyword is used to execute nothing; it means, when we don't want to execute code, the pass can be used to execute empty. It is the same as the name refers to. It just makes the control to pass by without executing any code. If we want to bypass any code pass statement can be used.

It is beneficial when a statement is required syntactically, but we want we don't want to execute or execute it later. The difference between the comments and pass is that, comments are entirely ignored by the Python interpreter, where the pass statement is not ignored.

Suppose we have a loop, and we do not want to execute right this moment, but we will execute in the future. Here we can use the pass.

Consider the following example.

Example - Pass statement

  1. # pass is just a placeholder for  
  2. # we will adde functionality later.  
  3. values = {'P''y''t''h','o','n'}  
  4. for val in values:  
  5.     pass  

Example - 2:

  1. for i in [1,2,3,4,5]:   
  2.     if(i==4):  
  3.         pass  
  4.         print("This is pass block",i)  
  5.     print(i)  

Output:

  1. 1  
  2. 2  
  3. 3  
  4. This is pass block 4  
  5. 4  
  6. 5  

We can create empty class or function using the pass statement.

  1. # Empty Function  
  2. def function_name(args):  
  3.     pass  
  4. #Empty Class  
  5. class Python:  
  6.     pass  

Next TopicPython Strings





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python String

Till now, we have discussed numbers as the standard data-types in Python. In this section of the tutorial, we will discuss the most popular data type in Python, i.e., string.

Python string is the collection of the characters surrounded by single quotes, double quotes, or triple quotes. The computer does not understand the characters; internally, it stores manipulated character as the combination of the 0's and 1's.

Each character is encoded in the ASCII or Unicode character. So we can say that Python strings are also called the collection of Unicode characters.

In Python, strings can be created by enclosing the character or the sequence of characters in the quotes. Python allows us to use single quotes, double quotes, or triple quotes to create the string.

Consider the following example in Python to create a string.

Syntax:

  1. str = "Hi Python !"    

Here, if we check the type of the variable str using a Python script

  1. print(type(str)), then it will print a string (str).    

In Python, strings are treated as the sequence of characters, which means that Python doesn't support the character data-type; instead, a single character written as 'p' is treated as the string of length 1.

Creating String in Python

We can create a string by enclosing the characters in single-quotes or double- quotes. Python also provides triple-quotes to represent the string, but it is generally used for multiline string or docstrings.

  1. #Using single quotes  
  2. str1 = 'Hello Python'  
  3. print(str1)  
  4. #Using double quotes  
  5. str2 = "Hello Python"  
  6. print(str2)  
  7.   
  8. #Using triple quotes  
  9. str3 = '''''Triple quotes are generally used for  
  10.     represent the multiline or 
  11.     docstring'''   
  12. print(str3)  

Output:

Hello Python
Hello Python
Triple quotes are generally used for
represent the multiline or
docstring

Strings indexing and splitting

Like other languages, the indexing of the Python strings starts from 0. For example, The string "HELLO" is indexed as given in the below figure.

Python String

Consider the following example:

  1. str = "HELLO"  
  2. print(str[0])  
  3. print(str[1])  
  4. print(str[2])  
  5. print(str[3])  
  6. print(str[4])  
  7. # It returns the IndexError because 6th index doesn't exist  
  8. print(str[6])  

Output:

H
E
L
L
O
IndexError: string index out of range

As shown in Python, the slice operator [] is used to access the individual characters of the string. However, we can use the : (colon) operator in Python to access the substring from the given string. Consider the following example.

Python String

Here, we must notice that the upper range given in the slice operator is always exclusive i.e., if str = 'HELLO' is given, then str[1:3] will always include str[1] = 'E', str[2] = 'L' and nothing else.

Consider the following example:

  1. # Given String  
  2. str = "JAVATPOINT"  
  3. # Start Oth index to end  
  4. print(str[0:])  
  5. # Starts 1th index to 4th index  
  6. print(str[1:5])  
  7. # Starts 2nd index to 3rd index  
  8. print(str[2:4])  
  9. # Starts 0th to 2nd index  
  10. print(str[:3])  
  11. #Starts 4th to 6th index  
  12. print(str[4:7])  

Output:

JAVATPOINT
AVAT
VA
JAV
TPO

We can do the negative slicing in the string; it starts from the rightmost character, which is indicated as -1. The second rightmost index indicates -2, and so on. Consider the following image.

Python String

Consider the following example

  1. str = 'JAVATPOINT'  
  2. print(str[-1])  
  3. print(str[-3])  
  4. print(str[-2:])  
  5. print(str[-4:-1])  
  6. print(str[-7:-2])  
  7. # Reversing the given string  
  8. print(str[::-1])  
  9. print(str[-12])  

Output:

T
I
NT
OIN
ATPOI
TNIOPTAVAJ
IndexError: string index out of range

Reassigning Strings

Updating the content of the strings is as easy as assigning it to a new string. The string object doesn't support item assignment i.e., A string can only be replaced with new string since its content cannot be partially replaced. Strings are immutable in Python.

Consider the following example.

Example 1

  1. str = "HELLO"    
  2. str[0] = "h"    
  3. print(str)    

Output:

Traceback (most recent call last):
File "12.py", line 2, in <module>
str[0] = "h";
TypeError: 'str' object does not support item assignment

However, in example 1, the string str can be assigned completely to a new content as specified in the following example.

Example 2

  1. str = "HELLO"    
  2. print(str)    
  3. str = "hello"    
  4. print(str)    

Output:

HELLO
hello

Deleting the String

As we know that strings are immutable. We cannot delete or remove the characters from the string.  But we can delete the entire string using the del keyword.

  1. str = "JAVATPOINT"  
  2. del str[1]  

Output:

TypeError: 'str' object doesn't support item deletion

Now we are deleting entire string.

  1. str1 = "JAVATPOINT"  
  2. del str1  
  3. print(str1)  

Output:

NameError: name 'str1' is not defined

String Operators

Operator Description
+ It is known as concatenation operator used to join the strings given either side of the operator.
* It is known as repetition operator. It concatenates the multiple copies of the same string.
[] It is known as slice operator. It is used to access the sub-strings of a particular string.
[:] It is known as range slice operator. It is used to access the characters from the specified range.
in It is known as membership operator. It returns if a particular sub-string is present in the specified string.
not in It is also a membership operator and does the exact reverse of in. It returns true if a particular substring is not present in the specified string.
r/R It is used to specify the raw string. Raw strings are used in the cases where we need to print the actual meaning of escape characters such as "C://python". To define any string as a raw string, the character r or R is followed by the string.
% It is used to perform string formatting. It makes use of the format specifiers used in C programming like %d or %f to map their values in python. We will discuss how formatting is done in python.

Example

Consider the following example to understand the real use of Python operators.

  1. str = "Hello"     
  2. str1 = " world"    
  3. print(str*3# prints HelloHelloHello    
  4. print(str+str1)# prints Hello world     
  5. print(str[4]) # prints o                
  6. print(str[2:4]); # prints ll                    
  7. print('w' in str) # prints false as w is not present in str    
  8. print('wo' not in str1) # prints false as wo is present in str1.     
  9. print(r'C://python37'# prints C://python37 as it is written    
  10. print("The string str : %s"%(str)) # prints The string str : Hello     

Output:

HelloHelloHello
Hello world
o
ll
False
False
C://python37
The string str : Hello

Python String Formatting

Escape Sequence

Let's suppose we need to write the text as - They said, "Hello what's going on?"- the given statement can be written in single quotes or double quotes but it will raise the SyntaxError as it contains both single and double-quotes.

Example

Consider the following example to understand the real use of Python operators.

  1. str = "They said, "Hello what's going on?""  
  2. print(str)  

Output:

SyntaxError: invalid syntax

We can use the triple quotes to accomplish this problem but Python provides the escape sequence.

The backslash(/) symbol denotes the escape sequence. The backslash can be followed by a special character and it interpreted differently. The single quotes inside the string must be escaped. We can apply the same as in the double quotes.

Example -

  1. # using triple quotes  
  2. print('''''They said, "What's there?"''')  
  3.   
  4. # escaping single quotes  
  5. print('They said, "What\'s going on?"')  
  6.   
  7. # escaping double quotes  
  8. print("They said, \"What's going on?\"")  

Output:

They said, "What's there?"
They said, "What's going on?"
They said, "What's going on?"

The list of an escape sequence is given below:

Sr. Escape Sequence Description Example
1. \newline It ignores the new line.
print("Python1 \
Python2 \
Python3")
Output:
Python1 Python2 Python3
2. \\ Backslash
print("\\")
Output:
\
3. \' Single Quotes
print('\'')
Output:
'
4. \\'' Double Quotes
print("\"")
Output:
"
5. \a ASCII Bell
print("\a")
6. \b ASCII Backspace(BS)
print("Hello \b World")
Output:
Hello World
7. \f ASCII Formfeed
print("Hello \f World!")
Hello World!
8. \n ASCII Linefeed
print("Hello \n World!")
Output:
Hello
World!
9. \r ASCII Carriege Return(CR)
print("Hello \r World!")
Output:
World!
10. \t ASCII Horizontal Tab
print("Hello \t World!")
Output:
Hello World!
11. \v ASCII Vertical Tab
print("Hello \v World!")
Output:
Hello
World!
12. \ooo Character with octal value
print("\110\145\154\154\157")
Output:
Hello
13 \xHH Character with hex value.
print("\x48\x65\x6c\x6c\x6f")
Output:
Hello

Here is the simple example of escape sequence.

  1. print("C:\\Users\\DEVANSH SHARMA\\Python32\\Lib")  
  2. print("This is the \n multiline quotes")  
  3. print("This is \x48\x45\x58 representation")  

Output:

C:\Users\DEVANSH SHARMA\Python32\Lib
This is the
multiline quotes
This is HEX representation

We can ignore the escape sequence from the given string by using the raw string. We can do this by writing r or R in front of the string. Consider the following example.

  1. print(r"C:\\Users\\DEVANSH SHARMA\\Python32")  

Output:

C:\\Users\\DEVANSH SHARMA\\Python32

The format() method

The format() method is the most flexible and useful method in formatting strings. The curly braces {} are used as the placeholder in the string and replaced by the format() method argument. Let's have a look at the given an example:

  1. # Using Curly braces  
  2. print("{} and {} both are the best friend".format("Devansh","Abhishek"))  
  3.   
  4. #Positional Argument  
  5. print("{1} and {0} best players ".format("Virat","Rohit"))  
  6.   
  7. #Keyword Argument  
  8. print("{a},{b},{c}".format(a = "James", b = "Peter", c = "Ricky"))  

Output:

Devansh and Abhishek both are the best friend
Rohit and Virat best players
James,Peter,Ricky

Python String Formatting Using % Operator

Python allows us to use the format specifiers used in C's printf statement. The format specifiers in Python are treated in the same way as they are treated in C. However, Python provides an additional operator %, which is used as an interface between the format specifiers and their values. In other words, we can say that it binds the format specifiers to the values.

Consider the following example.

  1. Integer = 10;    
  2. Float = 1.290    
  3. String = "Devansh"    
  4. print("Hi I am Integer ... My value is %d\nHi I am float ... My value is %f\nHi I am string ... My value is %s"%(Integer,Float,String))    

Output:

Hi I am Integer ... My value is 10
Hi I am float ... My value is 1.290000
Hi I am string ... My value is Devansh

Python String functions

Python provides various in-built functions that are used for string handling. Many String fun

Method Description
capitalize() It capitalizes the first character of the String. This function is deprecated in python3
casefold() It returns a version of s suitable for case-less comparisons.
center(width ,fillchar) It returns a space padded string with the original string centred with equal number of left and right spaces.
count(string,begin,end) It counts the number of occurrences of a substring in a String between begin and end index.
decode(encoding = 'UTF8', errors = 'strict') Decodes the string using codec registered for encoding.
encode() Encode S using the codec registered for encoding. Default encoding is 'utf-8'.
endswith(suffix ,begin=0,end=len(string)) It returns a Boolean value if the string terminates with given suffix between begin and end.
expandtabs(tabsize = 8) It defines tabs in string to multiple spaces. The default space value is 8.
find(substring ,beginIndex, endIndex) It returns the index value of the string where substring is found between begin index and end index.
format(value) It returns a formatted version of S, using the passed value.
index(subsring, beginIndex, endIndex) It throws an exception if string is not found. It works same as find() method.
isalnum() It returns true if the characters in the string are alphanumeric i.e., alphabets or numbers and there is at least 1 character. Otherwise, it returns false.
isalpha() It returns true if all the characters are alphabets and there is at least one character, otherwise False.
isdecimal() It returns true if all the characters of the string are decimals.
isdigit() It returns true if all the characters are digits and there is at least one character, otherwise False.
isidentifier() It returns true if the string is the valid identifier.
islower() It returns true if the characters of a string are in lower case, otherwise false.
isnumeric() It returns true if the string contains only numeric characters.
isprintable() It returns true if all the characters of s are printable or s is empty, false otherwise.
isupper() It returns false if characters of a string are in Upper case, otherwise False.
isspace() It returns true if the characters of a string are white-space, otherwise false.
istitle() It returns true if the string is titled properly and false otherwise. A title string is the one in which the first character is upper-case whereas the other characters are lower-case.
isupper() It returns true if all the characters of the string(if exists) is true otherwise it returns false.
join(seq) It merges the strings representation of the given sequence.
len(string) It returns the length of a string.
ljust(width[,fillchar]) It returns the space padded strings with the original string left justified to the given width.
lower() It converts all the characters of a string to Lower case.
lstrip() It removes all leading whitespaces of a string and can also be used to remove particular character from leading.
partition() It searches for the separator sep in S, and returns the part before it, the separator itself, and the part after it. If the separator is not found, return S and two empty strings.
maketrans() It returns a translation table to be used in translate function.
replace(old,new[,count]) It replaces the old sequence of characters with the new sequence. The max characters are replaced if max is given.
rfind(str,beg=0,end=len(str)) It is similar to find but it traverses the string in backward direction.
rindex(str,beg=0,end=len(str)) It is same as index but it traverses the string in backward direction.
rjust(width,[,fillchar]) Returns a space padded string having original string right justified to the number of characters specified.
rstrip() It removes all trailing whitespace of a string and can also be used to remove particular character from trailing.
rsplit(sep=None, maxsplit = -1) It is same as split() but it processes the string from the backward direction. It returns the list of words in the string. If Separator is not specified then the string splits according to the white-space.
split(str,num=string.count(str)) Splits the string according to the delimiter str. The string splits according to the space if the delimiter is not provided. It returns the list of substring concatenated with the delimiter.
splitlines(num=string.count('\n')) It returns the list of strings at each line with newline removed.
startswith(str,beg=0,end=len(str)) It returns a Boolean value if the string starts with given str between begin and end.
strip([chars]) It is used to perform lstrip() and rstrip() on the string.
swapcase() It inverts case of all characters in a string.
title() It is used to convert the string into the title-case i.e., The string meEruT will be converted to Meerut.
translate(table,deletechars = '') It translates the string according to the translation table passed in the function .
upper() It converts all the characters of a string to Upper Case.
zfill(width) Returns original string leftpadded with zeros to a total of width characters; intended for numbers, zfill() retains any sign given (less one zero).
rpartition()
Next TopicPython Lists





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python List

A list in Python is used to store the sequence of various types of data. Python lists are mutable type its mean we can modify its element after it created. However, Python consists of six data-types that are capable to store the sequences, but the most common and reliable type is the list.

A list can be defined as a collection of values or items of different types. The items in the list are separated with the comma (,) and enclosed with the square brackets [].

A list can be define as below

  1. L1 = ["John"102"USA"]    
  2. L2 = [123456]   

IIf we try to print the type of L1, L2, and L3 using type() function then it will come out to be a list.

  1. print(type(L1))  
  2. print(type(L2))  

Output:

<class 'list'>
<class 'list'>

Characteristics of Lists

The list has the following characteristics:

  • The lists are ordered.
  • The element of the list can access by index.
  • The lists are the mutable type.
  • The lists are mutable types.
  • A list can store the number of various elements.

Let's check the first statement that lists are the ordered.

  1. a = [1,2,"Peter",4.50,"Ricky",5,6]  
  2. b = [1,2,5,"Peter",4.50,"Ricky",6]  
  3. a ==b  

Output:

False

Both lists have consisted of the same elements, but the second list changed the index position of the 5th element that violates the order of lists. When compare both lists it returns the false.

Lists maintain the order of the element for the lifetime. That's why it is the ordered collection of objects.

  1. a = [12,"Peter"4.50,"Ricky",56]  
  2. b = [12,"Peter"4.50,"Ricky",56]  
  3. a == b  

Output:

True

Let's have a look at the list example in detail.

  1. emp = ["John"102"USA"]     
  2. Dep1 = ["CS",10]  
  3. Dep2 = ["IT",11]    
  4. HOD_CS = [10,"Mr. Holding"]    
  5. HOD_IT = [11"Mr. Bewon"]    
  6. print("printing employee data...")    
  7. print("Name : %s, ID: %d, Country: %s"%(emp[0],emp[1],emp[2]))    
  8. print("printing departments...")   
  9. print("Department 1:\nName: %s, ID: %d\nDepartment 2:\nName: %s, ID: %s"%(Dep1[0],Dep2[1],Dep2[0],Dep2[1]))    
  10. print("HOD Details ....")    
  11. print("CS HOD Name: %s, Id: %d"%(HOD_CS[1],HOD_CS[0]))    
  12. print("IT HOD Name: %s, Id: %d"%(HOD_IT[1],HOD_IT[0]))    
  13. print(type(emp),type(Dep1),type(Dep2),type(HOD_CS),type(HOD_IT))  

Output:

printing employee data...
Name : John, ID: 102, Country: USA
printing departments...
Department 1:
Name: CS, ID: 11
Department 2:
Name: IT, ID: 11
HOD Details ....
CS HOD Name: Mr. Holding, Id: 10
IT HOD Name: Mr. Bewon, Id: 11
<class 'list'> <class 'list'> <class 'list'> <class 'list'> <class 'list'>

In the above example, we have created the lists which consist of the employee and department details and printed the corresponding details. Observe the above code to understand the concept of the list better.

List indexing and splitting

The indexing is processed in the same way as it happens with the strings. The elements of the list can be accessed by using the slice operator [].

The index starts from 0 and goes to length - 1. The first element of the list is stored at the 0th index, the second element of the list is stored at the 1st index, and so on.

Python Lists

We can get the sub-list of the list using the following syntax.

  1. list_varible(start:stop:step)  
  • The start denotes the starting index position of the list.
  • The stop denotes the last index position of the list.
  • The step is used to skip the nth element within a start:stop

Consider the following example:

  1. list = [1,2,3,4,5,6,7]  
  2. print(list[0])  
  3. print(list[1])  
  4. print(list[2])  
  5. print(list[3])  
  6. # Slicing the elements  
  7. print(list[0:6])  
  8. # By default the index value is 0 so its starts from the 0th element and go for index -1.  
  9. print(list[:])  
  10. print(list[2:5])  
  11. print(list[1:6:2])  

Output:

1
2
3
4
[1, 2, 3, 4, 5, 6]
[1, 2, 3, 4, 5, 6, 7]
[3, 4, 5]
[2, 4, 6]

Unlike other languages, Python provides the flexibility to use the negative indexing also. The negative indices are counted from the right. The last element (rightmost) of the list has the index -1; its adjacent left element is present at the index -2 and so on until the left-most elements are encountered.

Python Lists

Let's have a look at the following example where we will use negative indexing to access the elements of the list.

  1. list = [1,2,3,4,5]  
  2. print(list[-1])  
  3. print(list[-3:])  
  4. print(list[:-1])  
  5. print(list[-3:-1])  

Output:

5
[3, 4, 5]
[1, 2, 3, 4]
[3, 4]

As we discussed above, we can get an element by using negative indexing. In the above code, the first print statement returned the rightmost element of the list. The second print statement returned the sub-list, and so on.

Updating List values

Lists are the most versatile data structures in Python since they are mutable, and their values can be updated by using the slice and assignment operator.

Python also provides append() and insert() methods, which can be used to add values to the list.

Consider the following example to update the values inside the list.

  1. list = [123456]     
  2. print(list)     
  3. # It will assign value to the value to the second index   
  4. list[2] = 10   
  5. print(list)    
  6. # Adding multiple-element   
  7. list[1:3] = [8978]     
  8. print(list)   
  9. # It will add value at the end of the list  
  10. list[-1] = 25  
  11. print(list)  

Output:

[1, 2, 3, 4, 5, 6]
[1, 2, 10, 4, 5, 6]
[1, 89, 78, 4, 5, 6]
[1, 89, 78, 4, 5, 25]

The list elements can also be deleted by using the del keyword. Python also provides us the remove() method if we do not know which element is to be deleted from the list.

Consider the following example to delete the list elements.

  1. list = [123456]     
  2. print(list)     
  3. # It will assign value to the value to second index   
  4. list[2] = 10   
  5. print(list)    
  6. # Adding multiple element   
  7. list[1:3] = [8978]     
  8. print(list)   
  9. # It will add value at the end of the list  
  10. list[-1] = 25  
  11. print(list)  

Output:

[1, 2, 3, 4, 5, 6]
[1, 2, 10, 4, 5, 6]
[1, 89, 78, 4, 5, 6]
[1, 89, 78, 4, 5, 25]

Python List Operations

The concatenation (+) and repetition (*) operators work in the same way as they were working with the strings.

Let's see how the list responds to various operators.

  1. Consider a Lists l1 = [1234], and l2 = [5678] to perform operation.  
Operator Description Example
Repetition The repetition operator enables the list elements to be repeated multiple times.
L1*2 = [1, 2, 3, 4, 1, 2, 3, 4]
Concatenation It concatenates the list mentioned on either side of the operator.
l1+l2 = [1, 2, 3, 4, 5, 6, 7, 8]
Membership It returns true if a particular item exists in a particular list otherwise false.
print(2 in l1) prints True.
Iteration The for loop is used to iterate over the list elements.
for i in l1:
print(i)
Output
1
2
3
4
Length It is used to get the length of the list
len(l1) = 4 

Iterating a List

A list can be iterated by using a for - in loop. A simple list containing four strings, which can be iterated as follows.

  1. list = ["John""David""James""Jonathan"]    
  2. for i in list:   
  3.     # The i variable will iterate over the elements of the List and contains each element in each iteration.     
  4.     print(i)  

Output:

John
David
James
Jonathan

Adding elements to the list

Python provides append() function which is used to add an element to the list. However, the append() function can only add value to the end of the list.

Consider the following example in which, we are taking the elements of the list from the user and printing the list on the console.

  1. #Declaring the empty list  
  2. l =[]  
  3. #Number of elements will be entered by the user    
  4. n = int(input("Enter the number of elements in the list:"))  
  5. # for loop to take the input  
  6. for i in range(0,n):     
  7.     # The input is taken from the user and added to the list as the item  
  8.     l.append(input("Enter the item:"))     
  9. print("printing the list items..")   
  10. # traversal loop to print the list items    
  11. for i in l:   
  12.     print(i, end = "  ")     

Output:

Enter the number of elements in the list:5
Enter the item:25
Enter the item:46
Enter the item:12
Enter the item:75
Enter the item:42
printing the list items
25 46 12 75 42

Removing elements from the list

Python provides the remove() function which is used to remove the element from the list. Consider the following example to understand this concept.

Example -

  1. list = [0,1,2,3,4]     
  2. print("printing original list: ");    
  3. for i in list:    
  4.     print(i,end=" ")    
  5. list.remove(2)    
  6. print("\nprinting the list after the removal of first element...")    
  7. for i in list:    
  8.     print(i,end=" ")  

Output:

printing original list:
0 1 2 3 4
printing the list after the removal of first element...
0 1 3 4

Python List Built-in functions

Python provides the following built-in functions, which can be used with the lists.

SN Function Description Example
1 cmp(list1, list2) It compares the elements of both the lists. This method is not used in the Python 3 and the above versions.
2 len(list) It is used to calculate the length of the list.
L1 = [1,2,3,4,5,6,7,8]
print(len(L1))
8
3 max(list) It returns the maximum element of the list.
L1 = [12,34,26,48,72]
print(max(L1))
72
4 min(list) It returns the minimum element of the list.
L1 = [12,34,26,48,72]
print(min(L1))
12
5 list(seq) It converts any sequence to the list.
str = "Johnson"
s = list(str)
print(type(s))
<class list>

Let's have a look at the few list examples.

Example: 1- Write the program to remove the duplicate element of the list.

  1. list1 = [1,2,2,3,55,98,65,65,13,29]  
  2. # Declare an empty list that will store unique values  
  3. list2 = []  
  4. for i in list1:  
  5.     if i not in list2:  
  6.         list2.append(i)  
  7. print(list2)  

Output:

[1, 2, 3, 55, 98, 65, 13, 29]

Example:2- Write a program to find the sum of the element in the list.

  1. list1 = [3,4,5,9,10,12,24]  
  2. sum = 0  
  3. for i in list1:  
  4.     sum = sum+i      
  5. print("The sum is:",sum)  

Output:

The sum is: 67

Example: 3- Write the program to find the lists consist of at least one common element.

  1. list1 = [1,2,3,4,5,6]  
  2. list2 = [7,8,9,2,10]  
  3. for x in list1:  
  4.     for y in list2:  
  5.         if x == y:  
  6.             print("The common element is:",x)  

Output:

The common element is: 2

Next TopicPython Tuples





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Tuple

Python Tuple is used to store the sequence of immutable Python objects. The tuple is similar to lists since the value of the items stored in the list can be changed, whereas the tuple is immutable, and the value of the items stored in the tuple cannot be changed.

Creating a tuple

A tuple can be written as the collection of comma-separated (,) values enclosed with the small () brackets. The parentheses are optional but it is good practice to use. A tuple can be defined as follows.

  1. T1 = (101"Peter"22)    
  2. T2 = ("Apple""Banana""Orange")     
  3. T3 = 10,20,30,40,50  
  4.   
  5. print(type(T1))  
  6. print(type(T2))  
  7. print(type(T3))  

Output:

<class 'tuple'>
<class 'tuple'>
<class 'tuple'>

Note: The tuple which is created without using parentheses is also known as tuple packing.

An empty tuple can be created as follows.

T4 = ()

Creating a tuple with single element is slightly different. We will need to put comma after the element to declare the tuple.

  1. tup1 = ("JavaTpoint")  
  2. print(type(tup1))  
  3. #Creating a tuple with single element   
  4. tup2 = ("JavaTpoint",)  
  5. print(type(tup2))  

Output:

<class 'str'>
<class 'tuple'>

A tuple is indexed in the same way as the lists. The items in the tuple can be accessed by using their specific index value.

Consider the following example of tuple:

Example - 1

  1. tuple1 = (102030405060)    
  2. print(tuple1)    
  3. count = 0    
  4. for i in tuple1:    
  5.     print("tuple1[%d] = %d"%(count, i))   
  6.     count = count+1  

Output:

(10, 20, 30, 40, 50, 60)
tuple1[0] = 10
tuple1[1] = 20
tuple1[2] = 30
tuple1[3] = 40
tuple1[4] = 50
tuple1[5] = 60

Example - 2

  1. tuple1 = tuple(input("Enter the tuple elements ..."))  
  2. print(tuple1)    
  3. count = 0    
  4. for i in tuple1:    
  5.     print("tuple1[%d] = %s"%(count, i))   
  6.     count = count+1  

Output:

Enter the tuple elements ...123456
('1', '2', '3', '4', '5', '6')
tuple1[0] = 1
tuple1[1] = 2
tuple1[2] = 3
tuple1[3] = 4
tuple1[4] = 5
tuple1[5] = 6

A tuple is indexed in the same way as the lists. The items in the tuple can be accessed by using their specific index value.

We will see all these aspects of tuple in this section of the tutorial.

Tuple indexing and slicing

The indexing and slicing in the tuple are similar to lists. The indexing in the tuple starts from 0 and goes to length(tuple) - 1.

The items in the tuple can be accessed by using the index [] operator. Python also allows us to use the colon operator to access multiple items in the tuple.

Consider the following image to understand the indexing and slicing in detail.

Python Tuple

Consider the following example:

  1. tup = (1,2,3,4,5,6,7)  
  2. print(tup[0])  
  3. print(tup[1])  
  4. print(tup[2])  
  5. # It will give the IndexError  
  6. print(tup[8])  

Output:

1
2
3
tuple index out of range

In the above code, the tuple has 7 elements which denote 0 to 6. We tried to access an element outside of tuple that raised an IndexError.

  1. tuple = (1,2,3,4,5,6,7)  
  2. #element 1 to end  
  3. print(tuple[1:])  
  4. #element 0 to 3 element   
  5. print(tuple[:4])  
  6. #element 1 to 4 element  
  7. print(tuple[1:5])   
  8. # element 0 to 6 and take step of 2  
  9. print(tuple[0:6:2])  

Output:

(2, 3, 4, 5, 6, 7)
(1, 2, 3, 4)
(1, 2, 3, 4)
(1, 3, 5)

Negative Indexing

The tuple element can also access by using negative indexing. The index of -1 denotes the rightmost element and -2 to the second last item and so on.

The elements from left to right are traversed using the negative indexing. Consider the following example:

  1. tuple1 = (12345)    
  2. print(tuple1[-1])    
  3. print(tuple1[-4])    
  4. print(tuple1[-3:-1])  
  5. print(tuple1[:-1])  
  6. print(tuple1[-2:])  

Output:

5
2
(3, 4)
(1, 2, 3, 4)
(4, 5)

Deleting Tuple

Unlike lists, the tuple items cannot be deleted by using the del keyword as tuples are immutable. To delete an entire tuple, we can use the del keyword with the tuple name.

Consider the following example.

  1. tuple1 = (123456)    
  2. print(tuple1)    
  3. del tuple1[0]    
  4. print(tuple1)    
  5. del tuple1    
  6. print(tuple1)    

Output:

(1, 2, 3, 4, 5, 6)
Traceback (most recent call last):
File "tuple.py", line 4, in <module>
print(tuple1)
NameError: name 'tuple1' is not defined

Basic Tuple operations

The operators like concatenation (+), repetition (*), Membership (in) works in the same way as they work with the list. Consider the following table for more detail.

Let's say Tuple t = (1, 2, 3, 4, 5) and Tuple t1 = (6, 7, 8, 9) are declared.

Operator Description Example
Repetition The repetition operator enables the tuple elements to be repeated multiple times.
T1*2 = (1, 2, 3, 4, 5, 1, 2, 3, 4, 5)
Concatenation It concatenates the tuple mentioned on either side of the operator.
T1+T2 = (1, 2, 3, 4, 5, 6, 7, 8, 9)
Membership It returns true if a particular item exists in the tuple otherwise false
print (2 in T1) prints True.
Iteration The for loop is used to iterate over the tuple elements.
for i in T1:
print(i)
Output
1
2
3
4
5
Length It is used to get the length of the tuple.
len(T1) = 5

Python Tuple inbuilt functions

SN Function Description
1 cmp(tuple1, tuple2) It compares two tuples and returns true if tuple1 is greater than tuple2 otherwise false.
2 len(tuple) It calculates the length of the tuple.
3 max(tuple) It returns the maximum element of the tuple
4 min(tuple) It returns the minimum element of the tuple.
5 tuple(seq) It converts the specified sequence to the tuple.

Where use tuple?

Using tuple instead of list is used in the following scenario.

1. Using tuple instead of list gives us a clear idea that tuple data is constant and must not be changed.

2. Tuple can simulate a dictionary without keys. Consider the following nested structure, which can be used as a dictionary.

  1. [(101"John"22), (102"Mike"28),  (103"Dustin"30)]  

List vs. Tuple

SN List Tuple
1 The literal syntax of list is shown by the []. The literal syntax of the tuple is shown by the ().
2 The List is mutable. The tuple is immutable.
3 The List has the a variable length. The tuple has the fixed length.
4 The list provides more functionality than a tuple. The tuple provides less functionality than the list.
5 The list is used in the scenario in which we need to store the simple collections with no constraints where the value of the items can be changed. The tuple is used in the cases where we need to store the read-only collections i.e., the value of the items cannot be changed. It can be used as the key inside the dictionary.
6 The lists are less memory efficient than a tuple. The tuples are more memory efficient because of its immutability.

Next TopicPython Set





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python List Vs Tuple

In this tutorial, we will learn the important difference between the list and tuples and how both are playing a significant role in Python.

Lists and Tuples are used to store one or more Python objects or data-types sequentially. Both can store any data such as integer, float, string, and dictionary. Lists and Tuples are similar in most factors but here we will describe the main difference between them.

Let's discuss the main differences in the following points.

Representation Differences

The representation of the Lists and tuple is marginally different. List are commonly enclosed with the square bracket [], and elements are comma-separated element. Tuples are enclosed with parenthesis (), and elements are separated by the comma. The parenthesis is optional to use, and these types of tuples are called tuple packing.

Consider the following example.

  1. list1 = ['JavaTpoint'1254.30, {'Name: ''Peter'}]  
  2. print(type(list))  
  3. tuple1 = ('JavaTpoint',5,8,31.9,[1,2,3])  
  4. print(type(tuple1))  

Output:

<class 'list'>
<class 'tuple'>

In the above program, we defined a list1 variable which holds a list of different data type from index 0 to 4. We defined another variable tuple1, which holds a tuple of different data types. It is enclosed by the ().

Mutable Lists and Immutable Tuples

It is the most important difference between list and tuple whereas lists are mutable, and tuples are immutable. The lists are mutable which means the Python object can be modified after creation, whereas tuples can't be modified after creation. Consider the given an example.

  1. a = ["Peter","Joseph","Mathew","Ricky"]  
  2. print(a)  

Output:

['Peter', 'Joseph', 'Mathew', 'Ricky']

Now we are changing 0th index element "Peter" to "Samson".

  1. a[0] = "Samson"  
  2. print(a)  

Output:

['Samson', 'Joseph', 'Mathew', 'Ricky']

Now we create a tuple and do the same thing.

  1. a = (10,20,"JavaTpoint",30,40)  
  2. print(a)  

Output:

(10, 20, 'JavaTpoint', 30, 40)

  1. a[0] = 50  

Output:

TypeError Traceback (most recent call last)
<ipython-input-5-52b2981fae12> in <module>
----> 1 a[0] = 50

TypeError: 'tuple' object does not support item assignment

We get an error while changing the 1st element of the tuple because of immutability. It does not support item assignment.

Debugging

The tuples are easy to debug in a big project because of its immutability. If we have a small project or less number of data, then lists play an effective role. Let's consider the following example:

  1. a = [6,9,4,3,7,0,1]  
  2. # Copying address of a in b  
  3. b = a  
  4. a[3] = "JavaToint"  
  5. print(a)  

Output:

[6, 9, 4, 'JavaToint', 7, 0, 1]

In the above code, we did b = a; here we are not copying the list object from b to a. The b referred to the address of the list a.  It means if we make the change in the b then that will reflect the same as in list a, and it makes debugging easy. But it is hard for the significant project where Python objects may have multiple references.

It will be very complicated to track those changes in lists but immutable object tuple can't change after created.

So tuples are easy to debug.

Functions Support

The tuples support less operation than the list. The inbuilt dir(object) is used to get all the supported functions for the list and tuple.

  • List Functions
  1. dir(list)  

Output:

['__add__','__class__','__contains__','__delattr__','__delitem__','__dir_,
'__doc__','__eq__','__format__', '__get__','__getattribute__','__getitem_' '__gt__','__hash__','__iadd__','__imul__','__init__','__init_subclass__''__iter__','__le__','__len__','__lt__','__mul__', '__ne__','__new__',
'__reduce__', '__reduce_ex__','__repr__','__reversed__','__rmul__','__setattr__','__setitem__','__sizeof__','__str__','__subclasshook__',
'append',
'clear',
'copy',
'count',
'extend',
'index',
'insert',
'pop',
'remove',
'reverse',
'sort']
  • Tuple Functions
  1. dir(tuple)  

Output:

['__add__',
'__class__',
'__contains__',
'__delattr__',
'__dir__',
'__doc__',
'__eq__',
'__format__',
'__ge__',
'__getattribute__',
'__getitem__',
'__getnewargs__',
'__gt__',
'__hash__',
'__init__',
'__init_subclass__',
'__iter__',
'__le__',
'__len__',
'__lt__',
'__mul__',
'__ne__',
'__new__',
'__reduce__',
'__reduce_ex__',
'__repr__',
'__rmul__',
'__setattr__',
'__sizeof__',
'__str__',
'__subclasshook__',
'count',
'index']

Memory Efficient

The tuples are more memory efficient than the list because tuple has less built-in operations. Lists are suitable for the fewer elements whereas tuples are a bit faster than the list for the huge amount of data.

  1. Tuple = (1,2,3,4,5,6,7,8,9,0,5485,87525,955,3343,53234,6423,623456,234535)  
  2. List = [1,2,3,4,5,6,7,8,9,0,78,34,43,32,43,55,54,212,642,533,43434,54532 ]  
  3. print('Tuple size =', Tuple.__sizeof__())       # Tuple size = 52  
  4. print('List size =', List.__sizeof__())    

Output:

Tuple size = 168
List size = 216

Conclusion

  • In Some cases, lists might seem more useful than tuples. But tuples are important data structures of the Python. Tuples are commonly used for unchangeable data or we can say that the data will be "write- protected" in the tuples. Tuples sends the indication to the Python interpreter the data should not change in the future.
  • We can use tuple the same as a dictionary without using keys to store the data. For example-
  1. list1 = [(101"Mike"24),(102'Hussey'26),(103'David'27),(104,  'Warner'29)]  
  • Tuples can use for the dictionary keys because these are hashable and immutable whereas lists can't use a keys in dictionary.
  1. dict = {("Mike",22):24000}    #valid dictionary  
  2. dict = {["Peter",26]:25000}   #Invalid dictionary  

Next TopicPython Set





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Set

A Python set is the collection of the unordered items. Each element in the set must be unique, immutable, and the sets remove the duplicate elements. Sets are mutable which means we can modify it after its creation.

Unlike other collections in Python, there is no index attached to the elements of the set, i.e., we cannot directly access any element of the set by the index. However, we can print them all together, or we can get the list of elements by looping through the set.

Creating a set

The set can be created by enclosing the comma-separated immutable items with the curly braces {}. Python also provides the set() method, which can be used to create the set by the passed sequence.

Example 1: Using curly braces

  1. Days = {"Monday""Tuesday""Wednesday""Thursday""Friday""Saturday""Sunday"}    
  2. print(Days)    
  3. print(type(Days))    
  4. print("looping through the set elements ... ")    
  5. for i in Days:    
  6.     print(i)    

Output:

{'Friday', 'Tuesday', 'Monday', 'Saturday', 'Thursday', 'Sunday', 'Wednesday'}
<class 'set'>
looping through the set elements ...
Friday
Tuesday
Monday
Saturday
Thursday
Sunday
Wednesday

Example 2: Using set() method

  1. Days = set(["Monday""Tuesday""Wednesday""Thursday""Friday""Saturday""Sunday"])    
  2. print(Days)    
  3. print(type(Days))    
  4. print("looping through the set elements ... ")    
  5. for i in Days:    
  6.     print(i)    

Output:

{'Friday', 'Wednesday', 'Thursday', 'Saturday', 'Monday', 'Tuesday', 'Sunday'}
<class 'set'>
looping through the set elements ...
Friday
Wednesday
Thursday
Saturday
Monday
Tuesday
Sunday

It can contain any type of element such as integer, float, tuple etc. But mutable elements (list, dictionary, set) can't be a member of set. Consider the following example.

  1. # Creating a set which have immutable elements  
  2. set1 = {1,2,3"JavaTpoint"20.514}  
  3. print(type(set1))  
  4. #Creating a set which have mutable element  
  5. set2 = {1,2,3,["Javatpoint",4]}  
  6. print(type(set2))  

Output:

<class 'set'>

Traceback (most recent call last)
<ipython-input-5-9605bb6fbc68> in <module>
4
5 #Creating a set which holds mutable elements
----> 6 set2 = {1,2,3,["Javatpoint",4]}
7 print(type(set2))

TypeError: unhashable type: 'list'

In the above code, we have created two sets, the set set1 have immutable elements and set2 have one mutable element as a list. While checking the type of set2, it raised an error, which means set can contain only immutable elements.

Creating an empty set is a bit different because empty curly {} braces are also used to create a dictionary as well. So Python provides the set() method used without an argument to create an empty set.

  1. # Empty curly braces will create dictionary  
  2. set3 = {}  
  3. print(type(set3))  
  4.   
  5. # Empty set using set() function  
  6. set4 = set()  
  7. print(type(set4))  

Output:

<class 'dict'>
<class 'set'>

Let's see what happened if we provide the duplicate element to the set.

  1. set5 = {1,2,4,4,5,8,9,9,10}  
  2. print("Return set with unique elements:",set5)  

Output:

Return set with unique elements: {1, 2, 4, 5, 8, 9, 10}

In the above code, we can see that set5 consisted of multiple duplicate elements when we printed it remove the duplicity from the set.

Adding items to the set

Python provides the add() method and update() method which can be used to add some particular item to the set. The add() method is used to add a single element whereas the update() method is used to add multiple elements to the set. Consider the following example.

Example: 1 - Using add() method

  1. Months = set(["January","February""March""April""May""June"])    
  2. print("\nprinting the original set ... ")    
  3. print(months)    
  4. print("\nAdding other months to the set...");    
  5. Months.add("July");    
  6. Months.add ("August");    
  7. print("\nPrinting the modified set...");    
  8. print(Months)    
  9. print("\nlooping through the set elements ... ")    
  10. for i in Months:    
  11.     print(i)    

Output:

printing the original set ...
{'February', 'May', 'April', 'March', 'June', 'January'}

Adding other months to the set...

Printing the modified set...
{'February', 'July', 'May', 'April', 'March', 'August', 'June', 'January'}

looping through the set elements ...
February
July
May
April
March
August
June
January

To add more than one item in the set, Python provides the update() method. It accepts iterable as an argument.

Consider the following example.

Example - 2 Using update() function

  1. Months = set(["January","February""March""April""May""June"])    
  2. print("\nprinting the original set ... ")    
  3. print(Months)    
  4. print("\nupdating the original set ... ")    
  5. Months.update(["July","August","September","October"]);    
  6. print("\nprinting the modified set ... ")     
  7. print(Months);  

Output:

printing the original set ...
{'January', 'February', 'April', 'May', 'June', 'March'}

updating the original set ...
printing the modified set ...
{'January', 'February', 'April', 'August', 'October', 'May', 'June', 'July', 'September', 'March'}

Removing items from the set

Python provides the discard() method and remove() method which can be used to remove the items from the set. The difference between these function, using discard() function if the item does not exist in the set then the set remain unchanged whereas remove() method will through an error.

Consider the following example.

Example-1 Using discard() method

  1. months = set(["January","February""March""April""May""June"])    
  2. print("\nprinting the original set ... ")    
  3. print(months)    
  4. print("\nRemoving some months from the set...");    
  5. months.discard("January");    
  6. months.discard("May");    
  7. print("\nPrinting the modified set...");    
  8. print(months)    
  9. print("\nlooping through the set elements ... ")    
  10. for i in months:    
  11.     print(i)    

Output:

printing the original set ...
{'February', 'January', 'March', 'April', 'June', 'May'}

Removing some months from the set...

Printing the modified set...
{'February', 'March', 'April', 'June'}

looping through the set elements ...
February
March
April
June

Python provides also the remove() method to remove the item from the set. Consider the following example to remove the items using remove() method.

Example-2 Using remove() function

  1. months = set(["January","February""March""April""May""June"])    
  2. print("\nprinting the original set ... ")    
  3. print(months)    
  4. print("\nRemoving some months from the set...");    
  5. months.remove("January");    
  6. months.remove("May");    
  7. print("\nPrinting the modified set...");    
  8. print(months)    

Output:

printing the original set ...
{'February', 'June', 'April', 'May', 'January', 'March'}

Removing some months from the set...

Printing the modified set...
{'February', 'June', 'April', 'March'}

We can also use the pop() method to remove the item. Generally, the pop() method will always remove the last item but the set is unordered, we can't determine which element will be popped from set.

Consider the following example to remove the item from the set using pop() method.

  1. Months = set(["January","February""March""April""May""June"])    
  2. print("\nprinting the original set ... ")    
  3. print(Months)    
  4. print("\nRemoving some months from the set...");    
  5. Months.pop();    
  6. Months.pop();    
  7. print("\nPrinting the modified set...");    
  8. print(Months)    

Output:

printing the original set ...
{'June', 'January', 'May', 'April', 'February', 'March'}

Removing some months from the set...

Printing the modified set...
{'May', 'April', 'February', 'March'}

In the above code, the last element of the Month set is March but the pop() method removed the June and January because the set is unordered and the pop() method could not determine the last element of the set.

Python provides the clear() method to remove all the items from the set.

Consider the following example.

  1. Months = set(["January","February""March""April""May""June"])    
  2. print("\nprinting the original set ... ")    
  3. print(Months)    
  4. print("\nRemoving all the items from the set...");    
  5. Months.clear()    
  6. print("\nPrinting the modified set...")    
  7. print(Months)    

Output:

printing the original set ...
{'January', 'May', 'June', 'April', 'March', 'February'}

Removing all the items from the set...

Printing the modified set...
set()

Difference between discard() and remove()

Despite the fact that discard() and remove() method both perform the same task, There is one main difference between discard() and remove().

If the key to be deleted from the set using discard() doesn't exist in the set, the Python will not give the error. The program maintains its control flow.

On the other hand, if the item to be deleted from the set using remove() doesn't exist in the set, the Python will raise an error.

Consider the following example.

Example-

  1. Months = set(["January","February""March""April""May""June"])    
  2. print("\nprinting the original set ... ")    
  3. print(Months)    
  4. print("\nRemoving items through discard() method...");    
  5. Months.discard("Feb"); #will not give an error although the key feb is not available in the set    
  6. print("\nprinting the modified set...")    
  7. print(Months)    
  8. print("\nRemoving items through remove() method...");    
  9. Months.remove("Jan"#will give an error as the key jan is not available in the set.     
  10. print("\nPrinting the modified set...")    
  11. print(Months)    

Output:

printing the original set ...
{'March', 'January', 'April', 'June', 'February', 'May'}

Removing items through discard() method...

printing the modified set...
{'March', 'January', 'April', 'June', 'February', 'May'}

Removing items through remove() method...
Traceback (most recent call last):
File "set.py", line 9, in
Months.remove("Jan")
KeyError: 'Jan'

Python Set Operations

Set can be performed mathematical operation such as union, intersection, difference, and symmetric difference. Python provides the facility to carry out these operations with operators or methods. We describe these operations as follows.

Union of two Sets

The union of two sets is calculated by using the pipe (|) operator. The union of the two sets contains all the items that are present in both the sets.

Python Set

Consider the following example to calculate the union of two sets.

Example 1: using union | operator

  1. Days1 = {"Monday","Tuesday","Wednesday","Thursday""Sunday"}    
  2. Days2 = {"Friday","Saturday","Sunday"}    
  3. print(Days1|Days2) #printing the union of the sets     

Output:

{'Friday', 'Sunday', 'Saturday', 'Tuesday', 'Wednesday', 'Monday', 'Thursday'}

Python also provides the union() method which can also be used to calculate the union of two sets. Consider the following example.

Example 2: using union() method

  1. Days1 = {"Monday","Tuesday","Wednesday","Thursday"}    
  2. Days2 = {"Friday","Saturday","Sunday"}    
  3. print(Days1.union(Days2)) #printing the union of the sets     

Output:

{'Friday', 'Monday', 'Tuesday', 'Thursday', 'Wednesday', 'Sunday', 'Saturday'}

Intersection of two sets

The intersection of two sets can be performed by the and & operator or the intersection() function. The intersection of the two sets is given as the set of the elements that common in both sets.

Python Set

Consider the following example.

Example 1: Using & operator

  1. Days1 = {"Monday","Tuesday""Wednesday""Thursday"}    
  2. Days2 = {"Monday","Tuesday","Sunday""Friday"}    
  3. print(Days1&Days2) #prints the intersection of the two sets    

Output:

{'Monday', 'Tuesday'}

Example 2: Using intersection() method

  1. set1 = {"Devansh","John""David""Martin"}    
  2. set2 = {"Steve""Milan""David""Martin"}    
  3. print(set1.intersection(set2)) #prints the intersection of the two sets    

Output:

{'Martin', 'David'}

Example 3:

  1. set1 = {1,2,3,4,5,6,7}  
  2. set2 = {1,2,20,32,5,9}  
  3. set3 = set1.intersection(set2)  
  4. print(set3)  

Output:

{1,2,5}

The intersection_update() method

The intersection_update() method removes the items from the original set that are not present in both the sets (all the sets if more than one are specified).

The intersection_update() method is different from the intersection() method since it modifies the original set by removing the unwanted items, on the other hand, the intersection() method returns a new set.

Consider the following example.

  1. a = {"Devansh""bob""castle"}    
  2. b = {"castle""dude""emyway"}    
  3. c = {"fuson""gaurav""castle"}    
  4.     
  5. a.intersection_update(b, c)    
  6.     
  7. print(a)    

Output:

{'castle'}

Difference between the two sets

The difference of two sets can be calculated by using the subtraction (-) operator or intersection() method. Suppose there are two sets A and B, and the difference is A-B that denotes the resulting set will be obtained that element of A, which is not present in the set B.

Python Set

Consider the following example.

Example 1 : Using subtraction ( - ) operator

  1. Days1 = {"Monday",  "Tuesday""Wednesday""Thursday"}    
  2. Days2 = {"Monday""Tuesday""Sunday"}    
  3. print(Days1-Days2) #{"Wednesday", "Thursday" will be printed}    

Output:

{'Thursday', 'Wednesday'}

Example 2 : Using difference() method

  1. Days1 = {"Monday",  "Tuesday""Wednesday""Thursday"}    
  2. Days2 = {"Monday""Tuesday""Sunday"}    
  3. print(Days1.difference(Days2)) # prints the difference of the two sets Days1 and Days2    

Output:

{'Thursday', 'Wednesday'}

Symmetric Difference of two sets

The symmetric difference of two sets is calculated by ^ operator or symmetric_difference() method. Symmetric difference of sets, it removes that element which is present in both sets. Consider the following example:

Python Set

Example - 1: Using ^ operator

  1. a = {1,2,3,4,5,6}  
  2. b = {1,2,9,8,10}  
  3. c = a^b  
  4. print(c)  

Output:

{3, 4, 5, 6, 8, 9, 10}

Example - 2: Using symmetric_difference() method

  1. a = {1,2,3,4,5,6}  
  2. b = {1,2,9,8,10}  
  3. c = a.symmetric_difference(b)  
  4. print(c)  

Output:

{3, 4, 5, 6, 8, 9, 10}

Set comparisons

Python allows us to use the comparison operators i.e., <, >, <=, >= , == with the sets by using which we can check whether a set is a subset, superset, or equivalent to other set. The boolean true or false is returned depending upon the items present inside the sets.

Consider the following example.

  1. Days1 = {"Monday",  "Tuesday""Wednesday""Thursday"}    
  2. Days2 = {"Monday""Tuesday"}    
  3. Days3 = {"Monday""Tuesday""Friday"}    
  4.     
  5. #Days1 is the superset of Days2 hence it will print true.     
  6. print (Days1>Days2)     
  7.     
  8. #prints false since Days1 is not the subset of Days2     
  9. print (Days1<Days2)    
  10.     
  11. #prints false since Days2 and Days3 are not equivalent     
  12. print (Days2 == Days3)    

Output:

True
False
False

FrozenSets

The frozen sets are the immutable form of the normal sets, i.e., the items of the frozen set cannot be changed and therefore it can be used as a key in the dictionary.

The elements of the frozen set cannot be changed after the creation. We cannot change or append the content of the frozen sets by using the methods like add() or remove().

The frozenset() method is used to create the frozenset object. The iterable sequence is passed into this method which is converted into the frozen set as a return type of the method.

Consider the following example to create the frozen set.

  1. Frozenset = frozenset([1,2,3,4,5])     
  2. print(type(Frozenset))    
  3. print("\nprinting the content of frozen set...")    
  4. for i in Frozenset:    
  5.     print(i);    
  6. Frozenset.add(6#gives an error since we cannot change the content of Frozenset after creation     

Output:

<class 'frozenset'>

printing the content of frozen set...
1
2
3
4
5
Traceback (most recent call last):
File "set.py", line 6, in <module>
Frozenset.add(6) #gives an error since we can change the content of Frozenset after creation
AttributeError: 'frozenset' object has no attribute 'add'

Frozenset for the dictionary

If we pass the dictionary as the sequence inside the frozenset() method, it will take only the keys from the dictionary and returns a frozenset that contains the key of the dictionary as its elements.

Consider the following example.

  1. Dictionary = {"Name":"John""Country":"USA""ID":101}     
  2. print(type(Dictionary))    
  3. Frozenset = frozenset(Dictionary); #Frozenset will contain the keys of the dictionary    
  4. print(type(Frozenset))    
  5. for i in Frozenset:     
  6.     print(i)    

Output:

<class 'dict'>
<class 'frozenset'>
Name
Country
ID

Set Programming Example

Example - 1: Write a program to remove the given number from the set.

  1. my_set = {1,2,3,4,5,6,12,24}  
  2. n = int(input("Enter the number you want to remove"))  
  3. my_set.discard(n)  
  4. print("After Removing:",my_set)  

Output:

Enter the number you want to remove:12
After Removing: {1, 2, 3, 4, 5, 6, 24}

Example - 2: Write a program to add multiple elements to the set.

  1. set1 = set([1,2,4,"John","CS"])  
  2. set1.update(["Apple","Mango","Grapes"])  
  3. print(set1)  

Output:

{1, 2, 4, 'Apple', 'John', 'CS', 'Mango', 'Grapes'}

Example - 3: Write a program to find the union between two set.

  1. set1 = set(["Peter","Joseph"65,59,96])  
  2. set2  = set(["Peter",1,2,"Joseph"])  
  3. set3 = set1.union(set2)  
  4. print(set3)  

Output:

{96, 65, 2, 'Joseph', 1, 'Peter', 59}

Example- 4: Write a program to find the intersection between two sets.

  1. set1 = {23,44,56,67,90,45,"Javatpoint"}  
  2. set2 = {13,23,56,76,"Sachin"}  
  3. set3 = set1.intersection(set2)  
  4. print(set3)  

Output:

{56, 23}

Example - 5: Write the program to add element to the frozenset.

  1. set1 = {23,44,56,67,90,45,"Javatpoint"}  
  2. set2 = {13,23,56,76,"Sachin"}  
  3. set3 = set1.intersection(set2)  
  4. print(set3)  

Output:

TypeError: 'frozenset' object does not support item assignment

Above code raised an error because frozensets are immutable and can't be changed after creation.

Example - 6: Write the program to find the issuperset, issubset and superset.

  1. set1 = set(["Peter","James","Camroon","Ricky","Donald"])  
  2. set2 = set(["Camroon","Washington","Peter"])  
  3. set3 = set(["Peter"])  
  4.   
  5. issubset = set1 >= set2  
  6. print(issubset)  
  7. issuperset = set1 <= set2  
  8. print(issuperset)  
  9. issubset = set3 <= set2  
  10. print(issubset)  
  11. issuperset = set2 >= set3  
  12. print(issuperset)  

Output:

False
False
True
True

Python Built-in set methods

Python contains the following methods to be used with the sets.

SN Method Description
1 add(item) It adds an item to the set. It has no effect if the item is already present in the set.
2 clear() It deletes all the items from the set.
3 copy() It returns a shallow copy of the set.
4 difference_update(....) It modifies this set by removing all the items that are also present in the specified sets.
5 discard(item) It removes the specified item from the set.
6 intersection() It returns a new set that contains only the common elements of both the sets. (all the sets if more than two are specified).
7 intersection_update(....) It removes the items from the original set that are not present in both the sets (all the sets if more than one are specified).
8 Isdisjoint(....) Return True if two sets have a null intersection.
9 Issubset(....) Report whether another set contains this set.
10 Issuperset(....) Report whether this set contains another set.
11 pop() Remove and return an arbitrary set element that is the last element of the set. Raises KeyError if the set is empty.
12 remove(item) Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError.
13 symmetric_difference(....) Remove an element from a set; it must be a member. If the element is not a member, raise a KeyError.
14 symmetric_difference_update(....) Update a set with the symmetric difference of itself and another.
15 union(....) Return the union of sets as a new set.
(i.e. all elements that are in either set.)
16 update() Update a set with the union of itself and others.

Next TopicPython Dictionary





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Dictionary

Python Dictionary is used to store the data in a key-value pair format. The dictionary is the data type in Python, which can simulate the real-life data arrangement where some specific value exists for some particular key. It is the mutable data-structure. The dictionary is defined into element Keys and values.

  • Keys must be a single element
  • Value can be any type such as list, tuple, integer, etc.

In other words, we can say that a dictionary is the collection of key-value pairs where the value can be any Python object. In contrast, the keys are the immutable Python object, i.e., Numbers, string, or tuple.

Creating the dictionary

The dictionary can be created by using multiple key-value pairs enclosed with the curly brackets {}, and each key is separated from its value by the colon (:).The syntax to define the dictionary is given below.

Syntax:

  1. Dict = {"Name""Tom""Age"22}    

In the above dictionary Dict, The keys Name and Age are the string that is an immutable object.

Let's see an example to create a dictionary and print its content.

  1. Employee = {"Name""John""Age"29"salary":25000,"Company":"GOOGLE"}    
  2. print(type(Employee))    
  3. print("printing Employee data .... ")    
  4. print(Employee)    

Output

<class 'dict'>
Printing Employee data ....
{'Name': 'John', 'Age': 29, 'salary': 25000, 'Company': 'GOOGLE'}

Python provides the built-in function dict() method which is also used to create dictionary. The empty curly braces {} is used to create empty dictionary.

  1. # Creating an empty Dictionary   
  2. Dict = {}   
  3. print("Empty Dictionary: ")   
  4. print(Dict)   
  5.   
  6. # Creating a Dictionary   
  7. # with dict() method   
  8. Dict = dict({1'Java'2'T'3:'Point'})   
  9. print("\nCreate Dictionary by using  dict(): ")   
  10. print(Dict)   
  11.   
  12. # Creating a Dictionary   
  13. # with each item as a Pair   
  14. Dict = dict([(1'Devansh'), (2'Sharma')])   
  15. print("\nDictionary with each item as a pair: ")   
  16. print(Dict)  

Output:

Empty Dictionary:
{}

Create Dictionary by using dict():
{1: 'Java', 2: 'T', 3: 'Point'}

Dictionary with each item as a pair:
{1: 'Devansh', 2: 'Sharma'}

Accessing the dictionary values

We have discussed how the data can be accessed in the list and tuple by using the indexing.

However, the values can be accessed in the dictionary by using the keys as keys are unique in the dictionary.

The dictionary values can be accessed in the following way.

  1. Employee = {"Name""John""Age"29"salary":25000,"Company":"GOOGLE"}  
  2. print(type(Employee))  
  3. print("printing Employee data .... ")  
  4. print("Name : %s" %Employee["Name"])  
  5. print("Age : %d" %Employee["Age"])  
  6. print("Salary : %d" %Employee["salary"])  
  7. print("Company : %s" %Employee["Company"])  

Output:

<class 'dict'>
printing Employee data ....
Name : John
Age : 29
Salary : 25000
Company : GOOGLE

Python provides us with an alternative to use the get() method to access the dictionary values. It would give the same result as given by the indexing.

Adding dictionary values

The dictionary is a mutable data type, and its values can be updated by using the specific keys. The value can be updated along with key Dict[key] = value. The update() method is also used to update an existing value.

Note: If the key-value already present in the dictionary, the value gets updated. Otherwise, the new keys added in the dictionary.

Let's see an example to update the dictionary values.

Example - 1:

  1. # Creating an empty Dictionary   
  2. Dict = {}   
  3. print("Empty Dictionary: ")   
  4. print(Dict)   
  5.     
  6. # Adding elements to dictionary one at a time   
  7. Dict[0] = 'Peter'  
  8. Dict[2] = 'Joseph'  
  9. Dict[3] = 'Ricky'  
  10. print("\nDictionary after adding 3 elements: ")   
  11. print(Dict)   
  12.     
  13. # Adding set of values    
  14. # with a single Key   
  15. # The Emp_ages doesn't exist to dictionary  
  16. Dict['Emp_ages'] = 203324  
  17. print("\nDictionary after adding 3 elements: ")   
  18. print(Dict)   
  19.     
  20. # Updating existing Key's Value   
  21. Dict[3] = 'JavaTpoint'  
  22. print("\nUpdated key value: ")   
  23. print(Dict)    

Output:

Empty Dictionary:
{}

Dictionary after adding 3 elements:
{0: 'Peter', 2: 'Joseph', 3: 'Ricky'}

Dictionary after adding 3 elements:
{0: 'Peter', 2: 'Joseph', 3: 'Ricky', 'Emp_ages': (20, 33, 24)}

Updated key value:
{0: 'Peter', 2: 'Joseph', 3: 'JavaTpoint', 'Emp_ages': (20, 33, 24)}

Example - 2:

  1. Employee = {"Name""John""Age"29"salary":25000,"Company":"GOOGLE"}    
  2. print(type(Employee))    
  3. print("printing Employee data .... ")    
  4. print(Employee)    
  5. print("Enter the details of the new employee....");    
  6. Employee["Name"] = input("Name: ");    
  7. Employee["Age"] = int(input("Age: "));    
  8. Employee["salary"] = int(input("Salary: "));    
  9. Employee["Company"] = input("Company:");    
  10. print("printing the new data");    
  11. print(Employee)    

Output:

Empty Dictionary:
{}

Dictionary after adding 3 elements:
{0: 'Peter', 2: 'Joseph', 3: 'Ricky'}

Dictionary after adding 3 elements:
{0: 'Peter', 2: 'Joseph', 3: 'Ricky', 'Emp_ages': (20, 33, 24)}

Updated key value:
{0: 'Peter', 2: 'Joseph', 3: 'JavaTpoint', 'Emp_ages': (20, 33, 24)}

Deleting elements using del keyword

The items of the dictionary can be deleted by using the del keyword as given below.

  1. Employee = {"Name""John""Age"29"salary":25000,"Company":"GOOGLE"}    
  2. print(type(Employee))    
  3. print("printing Employee data .... ")    
  4. print(Employee)    
  5. print("Deleting some of the employee data")     
  6. del Employee["Name"]    
  7. del Employee["Company"]    
  8. print("printing the modified information ")    
  9. print(Employee)    
  10. print("Deleting the dictionary: Employee");    
  11. del Employee    
  12. print("Lets try to print it again ");    
  13. print(Employee)    

Output:

<class 'dict'>
printing Employee data ....
{'Name': 'John', 'Age': 29, 'salary': 25000, 'Company': 'GOOGLE'}
Deleting some of the employee data
printing the modified information
{'Age': 29, 'salary': 25000}
Deleting the dictionary: Employee
Lets try to print it again
NameError: name 'Employee' is not defined

The last print statement in the above code, it raised an error because we tried to print the Employee dictionary that already deleted.

  • Using pop() method

The pop() method accepts the key as an argument and remove the associated value. Consider the following example.

  1. # Creating a Dictionary   
  2. Dict = {1'JavaTpoint'2'Peter'3'Thomas'}   
  3. # Deleting a key    
  4. # using pop() method   
  5. pop_ele = Dict.pop(3)   
  6. print(Dict)  

Output:

{1: 'JavaTpoint', 2: 'Peter'}

Python also provides a built-in methods popitem() and clear() method for remove elements from the dictionary. The popitem() removes the arbitrary element from a dictionary, whereas the clear() method removes all elements to the whole dictionary.

Iterating Dictionary

A dictionary can be iterated using for loop as given below.

Example 1

# for loop to print all the keys of a dictionary

  1. Employee = {"Name""John""Age"29"salary":25000,"Company":"GOOGLE"}    
  2. for x in Employee:    
  3.     print(x)  

Output:

Name
Age
salary
Company

Example 2

#for loop to print all the values of the dictionary

  1. Employee = {"Name""John""Age"29"salary":25000,"Company":"GOOGLE"}    
  2. for x in Employee:    
  3.     print(Employee[x])  

Output:

John
29
25000
GOOGLE

Example - 3

#for loop to print the values of the dictionary by using values() method.

  1. Employee = {"Name""John""Age"29"salary":25000,"Company":"GOOGLE"}    
  2. for x in Employee.values():    
  3.     print(x)  

Output:

John
29
25000
GOOGLE

Example 4

#for loop to print the items of the dictionary by using items() method.

  1. Employee = {"Name""John""Age"29"salary":25000,"Company":"GOOGLE"}    
  2. for x in Employee.items():    
  3.     print(x)  

Output:

('Name', 'John')
('Age', 29)
('salary', 25000)
('Company', 'GOOGLE')

Properties of Dictionary keys

1. In the dictionary, we cannot store multiple values for the same keys. If we pass more than one value for a single key, then the value which is last assigned is considered as the value of the key.

Consider the following example.

  1. Employee={"Name":"John","Age":29,"Salary":25000,"Company":"GOOGLE","Name":"John"}    
  2. for x,y in Employee.items():    
  3.     print(x,y)    

Output:

Name John
Age 29
Salary 25000
Company GOOGLE

2. In python, the key cannot be any mutable object. We can use numbers, strings, or tuples as the key, but we cannot use any mutable object like the list as the key in the dictionary.

Consider the following example.

  1. Employee = {"Name""John""Age"29"salary":25000,"Company":"GOOGLE",[100,201,301]:"Department ID"}    
  2. for x,y in Employee.items():    
  3.     print(x,y)    

Output:

Traceback (most recent call last):
File "dictionary.py", line 1, in
Employee = {"Name": "John", "Age": 29, "salary":25000,"Company":"GOOGLE",[100,201,301]:"Department ID"}
TypeError: unhashable type: 'list'

Built-in Dictionary functions

The built-in python dictionary methods along with the description are given below.

SN Function Description
1 cmp(dict1, dict2) It compares the items of both the dictionary and returns true if the first dictionary values are greater than the second dictionary, otherwise it returns false.
2 len(dict) It is used to calculate the length of the dictionary.
3 str(dict) It converts the dictionary into the printable string representation.
4 type(variable) It is used to print the type of the passed variable.

Built-in Dictionary methods

The built-in python dictionary methods along with the description are given below.

SN Method Description
1 dic.clear() It is used to delete all the items of the dictionary.
2 dict.copy() It returns a shallow copy of the dictionary.
3 dict.fromkeys(iterable, value = None, /) Create a new dictionary from the iterable with the values equal to value.
4 dict.get(key, default = "None") It is used to get the value specified for the passed key.
5 dict.has_key(key) It returns true if the dictionary contains the specified key.
6 dict.items() It returns all the key-value pairs as a tuple.
7 dict.keys() It returns all the keys of the dictionary.
8 dict.setdefault(key,default= "None") It is used to set the key to the default value if the key is not specified in the dictionary
9 dict.update(dict2) It updates the dictionary by adding the key-value pair of dict2 to this dictionary.
10 dict.values() It returns all the values of the dictionary.
11 len()
12 popItem()
13 pop()
14 count()
15 index()

Next TopicPython Functions





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Function

Functions are the most important aspect of an application. A function can be defined as the organized block of reusable code, which can be called whenever required.

Python allows us to divide a large program into the basic building blocks known as a function. The function contains the set of programming statements enclosed by {}. A function can be called multiple times to provide reusability and modularity to the Python program.

The Function helps to programmer to break the program into the smaller part. It organizes the code very effectively and avoids the repetition of the code. As the program grows, function makes the program more organized.

Python provide us various inbuilt functions like range() or print(). Although, the user can create its functions, which can be called user-defined functions.

There are mainly two types of functions.

  • User-define functions - The user-defined functions are those define by the user to perform the specific task.
  • Built-in functions - The built-in functions are those functions that are pre-defined in Python.

In this tutorial, we will discuss the user define functions.

Advantage of Functions in Python

There are the following advantages of Python functions.

  • Using functions, we can avoid rewriting the same logic/code again and again in a program.
  • We can call Python functions multiple times in a program and anywhere in a program.
  • We can track a large Python program easily when it is divided into multiple functions.
  • Reusability is the main achievement of Python functions.
  • However, Function calling is always overhead in a Python program.

Creating a Function

Python provides the def keyword to define the function. The syntax of the define function is given below.

Syntax:

  1. def my_function(parameters):  
  2.       function_block  
  3. return expression  

Let's understand the syntax of functions definition.

  • The def keyword, along with the function name is used to define the function.
  • The identifier rule must follow the function name.
  • A function accepts the parameter (argument), and they can be optional.
  • The function block is started with the colon (:), and block statements must be at the same indentation.
  • The return statement is used to return the value. A function can have only one return

Function Calling

In Python, after the function is created, we can call it from another function. A function must be defined before the function call; otherwise, the Python interpreter gives an error. To call the function, use the function name followed by the parentheses.

Consider the following example of a simple example that prints the message "Hello World".

  1. #function definition  
  2. def hello_world():    
  3.     print("hello world")    
  4. # function calling  
  5. hello_world()      

Output:

hello world

The return statement

The return statement is used at the end of the function and returns the result of the function. It terminates the function execution and transfers the result where the function is called. The return statement cannot be used outside of the function.

Syntax

  1. return [expression_list]  

It can contain the expression which gets evaluated and value is returned to the caller function. If the return statement has no expression or does not exist itself in the function then it returns the None object.

Consider the following example:

Example 1

  1. # Defining function  
  2. def sum():  
  3.     a = 10  
  4.     b = 20  
  5.     c = a+b  
  6.     return c  
  7. # calling sum() function in print statement  
  8. print("The sum is:",sum())  

Output:

The sum is: 30

In the above code, we have defined the function named sum, and it has a statement c = a+b, which computes the given values, and the result is returned by the return statement to the caller function.

Example 2 Creating function without return statement

  1. # Defining function  
  2. def sum():  
  3.     a = 10  
  4.     b = 20  
  5.     c = a+b  
  6. # calling sum() function in print statement  
  7. print(sum())  

Output:

None

In the above code, we have defined the same function without the return statement as we can see that the sum() function returned the None object to the caller function.

Arguments in function

The arguments are types of information which can be passed into the function. The arguments are specified in the parentheses. We can pass any number of arguments, but they must be separate them with a comma.

Consider the following example, which contains a function that accepts a string as the argument.

Example 1

  1. #defining the function    
  2. def func (name):    
  3.     print("Hi ",name)   
  4. #calling the function     
  5. func("Devansh")     

Output:

Hi Devansh

Example 2

  1. #Python function to calculate the sum of two variables     
  2. #defining the function    
  3. def sum (a,b):    
  4.     return a+b;    
  5.     
  6. #taking values from the user    
  7. a = int(input("Enter a: "))    
  8. b = int(input("Enter b: "))    
  9.     
  10. #printing the sum of a and b    
  11. print("Sum = ",sum(a,b))    

Output:

Enter a: 10
Enter b: 20
Sum = 30

Call by reference in Python

In Python, call by reference means passing the actual value as an argument in the function. All the functions are called by reference, i.e., all the changes made to the reference inside the function revert back to the original value referred by the reference.

Example 1 Passing Immutable Object (List)

  1. #defining the function    
  2. def change_list(list1):    
  3.     list1.append(20)   
  4.     list1.append(30)    
  5.     print("list inside function = ",list1)    
  6.     
  7. #defining the list    
  8. list1 = [10,30,40,50]    
  9.     
  10. #calling the function     
  11. change_list(list1)  
  12. print("list outside function = ",list1)  

Output:

list inside function = [10, 30, 40, 50, 20, 30]
list outside function = [10, 30, 40, 50, 20, 30]

Example 2 Passing Mutable Object (String)

  1. #defining the function    
  2. def change_string (str):    
  3.     str = str + " Hows you "  
  4.     print("printing the string inside function :",str)  
  5.     
  6. string1 = "Hi I am there"    
  7.     
  8. #calling the function    
  9. change_string(string1)    
  10.     
  11. print("printing the string outside function :",string1)    

Output:

printing the string inside function : Hi I am there Hows you
printing the string outside function : Hi I am there

Types of arguments

There may be several types of arguments which can be passed at the time of function call.

  1. Required arguments
  2. Keyword arguments
  3. Default arguments
  4. Variable-length arguments

Required Arguments

Till now, we have learned about function calling in Python. However, we can provide the arguments at the time of the function call. As far as the required arguments are concerned, these are the arguments which are required to be passed at the time of function calling with the exact match of their positions in the function call and function definition. If either of the arguments is not provided in the function call, or the position of the arguments is changed, the Python interpreter will show the error.

Consider the following example.

Example 1

  1. def func(name):    
  2.     message = "Hi "+name  
  3.     return message  
  4. name = input("Enter the name:")    
  5. print(func(name))    

Output:

Enter the name: John
Hi John

Example 2

  1. #the function simple_interest accepts three arguments and returns the simple interest accordingly    
  2. def simple_interest(p,t,r):    
  3.     return (p*t*r)/100    
  4. p = float(input("Enter the principle amount? "))    
  5. r = float(input("Enter the rate of interest? "))    
  6. t = float(input("Enter the time in years? "))    
  7. print("Simple Interest: ",simple_interest(p,r,t))    

Output:

Enter the principle amount: 5000
Enter the rate of interest: 5
Enter the time in years: 3
Simple Interest: 750.0

Example 3

  1. #the function calculate returns the sum of two arguments a and b    
  2. def calculate(a,b):    
  3.     return a+b    
  4. calculate(10# this causes an error as we are missing a required arguments b.    

Output:

TypeError: calculate() missing 1 required positional argument: 'b'

Default Arguments

Python allows us to initialize the arguments at the function definition. If the value of any of the arguments is not provided at the time of function call, then that argument can be initialized with the value given in the definition even if the argument is not specified at the function call.

Example 1

  1. def printme(name,age=22):    
  2.     print("My name is",name,"and age is",age)    
  3. printme(name = "john")  

Output:

My name is John and age is 22

Example 2

  1. def printme(name,age=22):    
  2.     print("My name is",name,"and age is",age)    
  3. printme(name = "john"#the variable age is not passed into the function however the default value of age is considered in the function    
  4. printme(age = 10,name="David"#the value of age is overwritten here, 10 will be printed as age   

Output:

My name is john and age is 22
My name is David and age is 10

Variable-length Arguments (*args)

In large projects, sometimes we may not know the number of arguments to be passed in advance. In such cases, Python provides us the flexibility to offer the comma-separated values which are internally treated as tuples at the function call. By using the variable-length arguments, we can pass any number of arguments.

However, at the function definition, we define the variable-length argument using the *args (star) as *<variable - name >.

Consider the following example.

Example

  1. def printme(*names):    
  2.     print("type of passed argument is ",type(names))    
  3.     print("printing the passed arguments...")    
  4.     for name in names:    
  5.         print(name)    
  6. printme("john","David","smith","nick")    

Output:

type of passed argument is <class 'tuple'>
printing the passed arguments...
john
David
smith
nick

In the above code, we passed *names as variable-length argument. We called the function and passed values which are treated as tuple internally. The tuple is an iterable sequence the same as the list. To print the given values, we iterated *arg names using for loop.

Keyword arguments(**kwargs)

Python allows us to call the function with the keyword arguments. This kind of function call will enable us to pass the arguments in the random order.

The name of the arguments is treated as the keywords and matched in the function calling and definition. If the same match is found, the values of the arguments are copied in the function definition.

Consider the following example.

Example 1

  1. #function func is called with the name and message as the keyword arguments    
  2. def func(name,message):    
  3.     print("printing the message with",name,"and ",message)    
  4.       
  5.     #name and message is copied with the values John and hello respectively    
  6.     func(name = "John",message="hello")   

Output:

printing the message with John and hello

Example 2 providing the values in different order at the calling

  1. #The function simple_interest(p, t, r) is called with the keyword arguments the order of arguments doesn't matter in this case    
  2. def simple_interest(p,t,r):    
  3.     return (p*t*r)/100    
  4. print("Simple Interest: ",simple_interest(t=10,r=10,p=1900))     

Output:

Simple Interest: 1900.0

If we provide the different name of arguments at the time of function call, an error will be thrown.

Consider the following example.

Example 3

  1. #The function simple_interest(p, t, r) is called with the keyword arguments.     
  2. def simple_interest(p,t,r):    
  3.     return (p*t*r)/100    
  4.   
  5. # doesn't find the exact match of the name of the arguments (keywords)      
  6. print("Simple Interest: ",simple_interest(time=10,rate=10,principle=1900))   

Output:

TypeError: simple_interest() got an unexpected keyword argument 'time'

The Python allows us to provide the mix of the required arguments and keyword arguments at the time of function call. However, the required argument must not be given after the keyword argument, i.e., once the keyword argument is encountered in the function call, the following arguments must also be the keyword arguments.

Consider the following example.

Example 4

  1. def func(name1,message,name2):    
  2.     print("printing the message with",name1,",",message,",and",name2)    
  3. #the first argument is not the keyword argument    
  4. func("John",message="hello",name2="David")   

Output:

printing the message with John , hello ,and David

The following example will cause an error due to an in-proper mix of keyword and required arguments being passed in the function call.

Example 5

  1. def func(name1,message,name2):   
  2.     print("printing the message with",name1,",",message,",and",name2)    
  3. func("John",message="hello","David")        

Output:

SyntaxError: positional argument follows keyword argument

Python provides the facility to pass the multiple keyword arguments which can be represented as **kwargs. It is similar as the *args but it stores the argument in the dictionary format.

This type of arguments is useful when we do not know the number of arguments in advance.

Consider the following example:

Example 6: Many arguments using Keyword argument

  1. def food(**kwargs):  
  2.     print(kwargs)  
  3. food(a="Apple")  
  4. food(fruits="Orange", Vagitables="Carrot")  

Output:

{'a': 'Apple'}
{'fruits': 'Orange', 'Vagitables': 'Carrot'}

Scope of variables

The scopes of the variables depend upon the location where the variable is being declared. The variable declared in one part of the program may not be accessible to the other parts.

In python, the variables are defined with the two types of scopes.

  1. Global variables
  2. Local variables

The variable defined outside any function is known to have a global scope, whereas the variable defined inside a function is known to have a local scope.

Consider the following example.

Example 1 Local Variable

  1. def print_message():    
  2.     message = "hello !! I am going to print a message." # the variable message is local to the function itself    
  3.     print(message)    
  4. print_message()    
  5. print(message) # this will cause an error since a local variable cannot be accessible here.      

Output:

hello !! I am going to print a message.
File "/root/PycharmProjects/PythonTest/Test1.py", line 5, in
print(message)
NameError: name 'message' is not defined

Example 2 Global Variable

  1. def calculate(*args):    
  2.     sum=0    
  3.     for arg in args:    
  4.         sum = sum +arg    
  5.     print("The sum is",sum)    
  6. sum=0    
  7. calculate(10,20,30#60 will be printed as the sum    
  8. print("Value of sum outside the function:",sum) # 0 will be printed  Output:  

Output:

The sum is 60
Value of sum outside the function: 0






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Built-in Functions

The Python built-in functions are defined as the functions whose functionality is pre-defined in Python. The python interpreter has several functions that are always present for use. These functions are known as Built-in Functions. There are several built-in functions in Python which are listed below:

Python abs() Function

The python abs() function is used to return the absolute value of a number. It takes only one argument, a number whose absolute value is to be returned. The argument can be an integer and floating-point number. If the argument is a complex number, then, abs() returns its magnitude.

Python abs() Function Example

  1. #  integer number     
  2. integer = -20  
  3. print('Absolute value of -40 is:', abs(integer))  
  4.   
  5. #  floating number  
  6. floating = -20.83  
  7. print('Absolute value of -40.83 is:', abs(floating))  

Output:

Absolute value of -20 is: 20
Absolute value of -20.83 is: 20.83

Python all() Function

The python all() function accepts an iterable object (such as list, dictionary, etc.). It returns true if all items in passed iterable are true. Otherwise, it returns False. If the iterable object is empty, the all() function returns True.

Python all() Function Example

  1. # all values true  
  2. k = [1346]  
  3. print(all(k))  
  4.   
  5. # all values false  
  6. k = [0False]  
  7. print(all(k))  
  8.   
  9. # one false value  
  10. k = [1370]  
  11. print(all(k))  
  12.   
  13. # one true value  
  14. k = [0False5]  
  15. print(all(k))  
  16.   
  17. # empty iterable  
  18. k = []  
  19. print(all(k))  

Output:

True
False
False
False
True

Python bin() Function

The python bin() function is used to return the binary representation of a specified integer. A result always starts with the prefix 0b.

Python bin() Function Example

  1. x =  10  
  2. y =  bin(x)  
  3. print (y)  

Output:

0b1010

Python bool()

The python bool() converts a value to boolean(True or False) using the standard truth testing procedure.

Python bool() Example

  1. test1 = []  
  2. print(test1,'is',bool(test1))  
  3. test1 = [0]  
  4. print(test1,'is',bool(test1))  
  5. test1 = 0.0  
  6. print(test1,'is',bool(test1))  
  7. test1 = None  
  8. print(test1,'is',bool(test1))  
  9. test1 = True  
  10. print(test1,'is',bool(test1))  
  11. test1 = 'Easy string'  
  12. print(test1,'is',bool(test1))  

Output:

[] is False
[0] is True
0.0 is False
None is False
True is True
Easy string is True

Python bytes()

The python bytes() in Python is used for returning a bytes object. It is an immutable version of the bytearray() function.

It can create empty bytes object of the specified size.

Python bytes() Example

  1. string = "Hello World."  
  2. array = bytes(string, 'utf-8')  
  3. print(array)  

Output:

b ' Hello World.'

Python callable() Function

A python callable() function in Python is something that can be called. This built-in function checks and returns true if the object passed appears to be callable, otherwise false.

Python callable() Function Example

  1. x = 8  
  2. print(callable(x))  

Output:

False

Python compile() Function

The python compile() function takes source code as input and returns a code object which can later be executed by exec() function.

Python compile() Function Example

  1. # compile string source to code  
  2. code_str = 'x=5\ny=10\nprint("sum =",x+y)'  
  3. code = compile(code_str, 'sum.py''exec')  
  4. print(type(code))  
  5. exec(code)  
  6. exec(x)  

Output:

<class 'code'>
sum = 15

Python exec() Function

The python exec() function is used for the dynamic execution of Python program which can either be a string or object code and it accepts large blocks of code, unlike the eval() function which only accepts a single expression.

Python exec() Function Example

  1. x = 8  
  2. exec('print(x==8)')  
  3. exec('print(x+4)')  

Output:

True
12

Python sum() Function

As the name says, python sum() function is used to get the sum of numbers of an iterable, i.e., list.

Python sum() Function Example

  1. s = sum([12,4 ])  
  2. print(s)  
  3.   
  4. s = sum([124], 10)  
  5. print(s)  

Output:

7
17

Python any() Function

The python any() function returns true if any item in an iterable is true. Otherwise, it returns False.

Python any() Function Example

  1. l = [4320]                              
  2. print(any(l))                                   
  3.   
  4. l = [0False]  
  5. print(any(l))  
  6.   
  7. l = [0False5]  
  8. print(any(l))  
  9.   
  10. l = []  
  11. print(any(l))  

Output:

True
False
True
False

Python ascii() Function

The python ascii() function returns a string containing a printable representation of an object and escapes the non-ASCII characters in the string using \x, \u or \U escapes.

Python ascii() Function Example

  1. normalText = 'Python is interesting'  
  2. print(ascii(normalText))  
  3.   
  4. otherText = 'Pythön is interesting'  
  5. print(ascii(otherText))  
  6.   
  7. print('Pyth\xf6n is interesting')  

Output:

'Python is interesting'
'Pyth\xf6n is interesting'
Pythön is interesting

Python bytearray()

The python bytearray() returns a bytearray object and can convert objects into bytearray objects, or create an empty bytearray object of the specified size.

Python bytearray() Example

  1. string = "Python is a programming language."  
  2.   
  3. # string with encoding 'utf-8'  
  4. arr = bytearray(string, 'utf-8')  
  5. print(arr)  

Output:

bytearray(b'Python is a programming language.')

Python eval() Function

The python eval() function parses the expression passed to it and runs python expression(code) within the program.

Python eval() Function Example

  1. x = 8  
  2. print(eval('x + 1'))  

Output:

9

Python float()

The python float() function returns a floating-point number from a number or string.

Python float() Example

  1. # for integers  
  2. print(float(9))  
  3.   
  4. # for floats  
  5. print(float(8.19))  
  6.   
  7. # for string floats  
  8. print(float("-24.27"))  
  9.   
  10. # for string floats with whitespaces  
  11. print(float("     -17.19\n"))  
  12.   
  13. # string float error  
  14. print(float("xyz"))  

Output:

9.0
8.19
-24.27
-17.19
ValueError: could not convert string to float: 'xyz'

Python format() Function

The python format() function returns a formatted representation of the given value.

Python format() Function Example

  1. # d, f and b are a type  
  2.   
  3. # integer  
  4. print(format(123"d"))  
  5.   
  6. # float arguments  
  7. print(format(123.4567898"f"))  
  8.   
  9. # binary format  
  10. print(format(12"b"))  

Output:

123
123.456790
1100

Python frozenset()

The python frozenset() function returns an immutable frozenset object initialized with elements from the given iterable.

Python frozenset() Example

  1. # tuple of letters  
  2. letters = ('m''r''o''t''s')  
  3.   
  4. fSet = frozenset(letters)  
  5. print('Frozen set is:', fSet)  
  6. print('Empty frozen set is:', frozenset())  

Output:

Frozen set is: frozenset({'o', 'm', 's', 'r', 't'})
Empty frozen set is: frozenset()

Python getattr() Function

The python getattr() function returns the value of a named attribute of an object. If it is not found, it returns the default value.

Python getattr() Function Example

  1. class Details:  
  2.     age = 22  
  3.     name = "Phill"  
  4.   
  5. details = Details()  
  6. print('The age is:', getattr(details, "age"))  
  7. print('The age is:', details.age)  

Output:

The age is: 22
The age is: 22

Python globals() Function

The python globals() function returns the dictionary of the current global symbol table.

A Symbol table is defined as a data structure which contains all the necessary information about the program. It includes variable names, methods, classes, etc.

Python globals() Function Example

  1. age = 22  
  2.   
  3. globals()['age'] = 22  
  4. print('The age is:', age)  

Output:

The age is: 22

Python hasattr() Function

The python any() function returns true if any item in an iterable is true, otherwise it returns False.

Python hasattr() Function Example

  1. l = [4320]                              
  2. print(any(l))                                   
  3.   
  4. l = [0False]  
  5. print(any(l))  
  6.   
  7. l = [0False5]  
  8. print(any(l))  
  9.   
  10. l = []  
  11. print(any(l))  

Output:

True
False
True
False

Python iter() Function

The python iter() function is used to return an iterator object. It creates an object which can be iterated one element at a time.

Python iter() Function Example

  1. # list of numbers  
  2. list = [1,2,3,4,5]  
  3.   
  4. listIter = iter(list)  
  5.   
  6. # prints '1'  
  7. print(next(listIter))  
  8.   
  9. # prints '2'  
  10. print(next(listIter))  
  11.   
  12. # prints '3'  
  13. print(next(listIter))  
  14.   
  15. # prints '4'  
  16. print(next(listIter))  
  17.   
  18. # prints '5'  
  19. print(next(listIter))  

Output:

1
2
3
4
5

Python len() Function

The python len() function is used to return the length (the number of items) of an object.

Python len() Function Example

  1. strA = 'Python'  
  2. print(len(strA))  

Output:

6

Python list()

The python list() creates a list in python.

Python list() Example

  1. # empty list  
  2. print(list())  
  3.   
  4. # string  
  5. String = 'abcde'       
  6. print(list(String))  
  7.   
  8. # tuple  
  9. Tuple = (1,2,3,4,5)  
  10. print(list(Tuple))  
  11. # list  
  12. List = [1,2,3,4,5]  
  13. print(list(List))  

Output:

[]
['a', 'b', 'c', 'd', 'e']
[1,2,3,4,5]
[1,2,3,4,5]

Python locals() Function

The python locals() method updates and returns the dictionary of the current local symbol table.

A Symbol table is defined as a data structure which contains all the necessary information about the program. It includes variable names, methods, classes, etc.

Python locals() Function Example

  1. def localsAbsent():  
  2.     return locals()  
  3.   
  4. def localsPresent():  
  5.     present = True  
  6.     return locals()  
  7.   
  8. print('localsNotPresent:', localsAbsent())  
  9. print('localsPresent:', localsPresent())  

Output:

localsAbsent: {}
localsPresent: {'present': True}

Python map() Function

The python map() function is used to return a list of results after applying a given function to each item of an iterable(list, tuple etc.).

Python map() Function Example

  1. def calculateAddition(n):  
  2.   return n+n  
  3.   
  4. numbers = (1234)  
  5. result = map(calculateAddition, numbers)  
  6. print(result)  
  7.   
  8. # converting map object to set  
  9. numbersAddition = set(result)  
  10. print(numbersAddition)  

Output:

<map object at 0x7fb04a6bec18>
{8, 2, 4, 6}

Python memoryview() Function

The python memoryview() function returns a memoryview object of the given argument.

Python memoryview () Function Example

  1. #A random bytearray  
  2. randomByteArray = bytearray('ABC''utf-8')  
  3.   
  4. mv = memoryview(randomByteArray)  
  5.   
  6. # access the memory view's zeroth index  
  7. print(mv[0])  
  8.   
  9. # It create byte from memory view  
  10. print(bytes(mv[0:2]))  
  11.   
  12. # It create list from memory view  
  13. print(list(mv[0:3]))  

Output:

65
b'AB'
[65, 66, 67]

Python object()

The python object() returns an empty object. It is a base for all the classes and holds the built-in properties and methods which are default for all the classes.

Python object() Example

  1. python = object()  
  2.   
  3. print(type(python))  
  4. print(dir(python))  

Output:

<class 'object'>
['__class__', '__delattr__', '__dir__', '__doc__', '__eq__', '__format__', '__ge__',
'__getattribute__', '__gt__', '__hash__', '__init__', '__le__', '__lt__', '__ne__',
'__new__', '__reduce__', '__reduce_ex__', '__repr__', '__setattr__', '__sizeof__',
'__str__', '__subclasshook__']

Python open() Function

The python open() function opens the file and returns a corresponding file object.

Python open() Function Example

  1. # opens python.text file of the current directory  
  2. f = open("python.txt")  
  3. # specifying full path  
  4. f = open("C:/Python33/README.txt")  

Output:

Since the mode is omitted, the file is opened in 'r' mode; opens for reading.

Python chr() Function

Python chr() function is used to get a string representing a character which points to a Unicode code integer. For example, chr(97) returns the string 'a'. This function takes an integer argument and throws an error if it exceeds the specified range. The standard range of the argument is from 0 to 1,114,111.

Python chr() Function Example

  1. # Calling function  
  2. result = chr(102# It returns string representation of a char  
  3. result2 = chr(112)  
  4. # Displaying result  
  5. print(result)  
  6. print(result2)  
  7. # Verify, is it string type?  
  8. print("is it string type:", type(result) is str)  

Output:

ValueError: chr() arg not in range(0x110000)

Python complex()

Python complex() function is used to convert numbers or string into a complex number. This method takes two optional parameters and returns a complex number. The first parameter is called a real and second as imaginary parts.

Python complex() Example

  1. # Python complex() function example  
  2. # Calling function  
  3. a = complex(1# Passing single parameter  
  4. b = complex(1,2# Passing both parameters  
  5. # Displaying result  
  6. print(a)  
  7. print(b)  

Output:

(1.5+0j)
(1.5+2.2j)

Python delattr() Function

Python delattr() function is used to delete an attribute from a class. It takes two parameters, first is an object of the class and second is an attribute which we want to delete. After deleting the attribute, it no longer available in the class and throws an error if try to call it using the class object.

Python delattr() Function Example

  1. class Student:  
  2.     id = 101  
  3.     name = "Pranshu"  
  4.     email = "pranshu@abc.com"  
  5. # Declaring function  
  6.     def getinfo(self):  
  7.         print(self.id, self.name, self.email)  
  8. s = Student()  
  9. s.getinfo()  
  10. delattr(Student,'course'# Removing attribute which is not available  
  11. s.getinfo() # error: throws an error  

Output:

101 Pranshu pranshu@abc.com
AttributeError: course

Python dir() Function

Python dir() function returns the list of names in the current local scope. If the object on which method is called has a method named __dir__(), this method will be called and must return the list of attributes. It takes a single object type argument.

Python dir() Function Example

  1. # Calling function  
  2. att = dir()  
  3. # Displaying result  
  4. print(att)  

Output:

['__annotations__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__',
'__name__', '__package__', '__spec__']

Python divmod() Function

Python divmod() function is used to get remainder and quotient of two numbers. This function takes two numeric arguments and returns a tuple. Both arguments are required and numeric

Python divmod() Function Example

  1. # Python divmod() function example  
  2. # Calling function  
  3. result = divmod(10,2)  
  4. # Displaying result  
  5. print(result)  

Output:

(5, 0)

Python enumerate() Function

Python enumerate() function returns an enumerated object. It takes two parameters, first is a sequence of elements and the second is the start index of the sequence. We can get the elements in sequence either through a loop or next() method.

Python enumerate() Function Example

  1. # Calling function  
  2. result = enumerate([1,2,3])  
  3. # Displaying result  
  4. print(result)  
  5. print(list(result))  

Output:

<enumerate object at 0x7ff641093d80>
[(0, 1), (1, 2), (2, 3)]

Python dict()

Python dict() function is a constructor which creates a dictionary. Python dictionary provides three different constructors to create a dictionary:

  • If no argument is passed, it creates an empty dictionary.
  • If a positional argument is given, a dictionary is created with the same key-value pairs. Otherwise, pass an iterable object.
  • If keyword arguments are given, the keyword arguments and their values are added to the dictionary created from the positional argument.

Python dict() Example

  1. # Calling function  
  2. result = dict() # returns an empty dictionary  
  3. result2 = dict(a=1,b=2)  
  4. # Displaying result  
  5. print(result)  
  6. print(result2)  

Output:

{}
{'a': 1, 'b': 2}

Python filter() Function

Python filter() function is used to get filtered elements. This function takes two arguments, first is a function and the second is iterable. The filter function returns a sequence of those elements of iterable object for which function returns true value.

The first argument can be none, if the function is not available and returns only elements that are true.

Python filter() Function Example

  1. # Python filter() function example  
  2. def filterdata(x):  
  3.     if x>5:  
  4.         return x  
  5. # Calling function  
  6. result = filter(filterdata,(1,2,6))  
  7. # Displaying result  
  8. print(list(result))  

Output:

[6]

Python hash() Function

Python hash() function is used to get the hash value of an object. Python calculates the hash value by using the hash algorithm. The hash values are integers and used to compare dictionary keys during a dictionary lookup. We can hash only the types which are given below:

Hashable types: * bool * int * long * float * string * Unicode * tuple * code object.

Python hash() Function Example

  1. # Calling function  
  2. result = hash(21# integer value  
  3. result2 = hash(22.2# decimal value  
  4. # Displaying result  
  5. print(result)  
  6. print(result2)  

Output:

21
461168601842737174

Python help() Function

Python help() function is used to get help related to the object passed during the call. It takes an optional parameter and returns help information. If no argument is given, it shows the Python help console. It internally calls python's help function.

Python help() Function Example

  1. # Calling function  
  2. info = help() # No argument  
  3. # Displaying result  
  4. print(info)  

Output:

Welcome to Python 3.5's help utility!

Python min() Function

Python min() function is used to get the smallest element from the collection. This function takes two arguments, first is a collection of elements and second is key, and returns the smallest element from the collection.

Python min() Function Example

  1. # Calling function  
  2. small = min(2225,325,2025# returns smallest element  
  3. small2 = min(1000.25,2025.35,5625.36,10052.50)  
  4. # Displaying result  
  5. print(small)  
  6. print(small2)  

Output:

325
1000.25

Python set() Function

In python, a set is a built-in class, and this function is a constructor of this class. It is used to create a new set using elements passed during the call. It takes an iterable object as an argument and returns a new set object.

Python set() Function Example

  1. # Calling function  
  2. result = set() # empty set  
  3. result2 = set('12')  
  4. result3 = set('javatpoint')  
  5. # Displaying result  
  6. print(result)  
  7. print(result2)  
  8. print(result3)  

Output:

set()
{'1', '2'}
{'a', 'n', 'v', 't', 'j', 'p', 'i', 'o'}

Python hex() Function

Python hex() function is used to generate hex value of an integer argument. It takes an integer argument and returns an integer converted into a hexadecimal string. In case, we want to get a hexadecimal value of a float, then use float.hex() function.

Python hex() Function Example

  1. # Calling function  
  2. result = hex(1)   
  3. # integer value  
  4. result2 = hex(342)   
  5. # Displaying result  
  6. print(result)  
  7. print(result2)  

Output:

0x1
0x156

Python id() Function

Python id() function returns the identity of an object. This is an integer which is guaranteed to be unique. This function takes an argument as an object and returns a unique integer number which represents identity. Two objects with non-overlapping lifetimes may have the same id() value.

Python id() Function Example

  1. # Calling function  
  2. val = id("Javatpoint"# string object  
  3. val2 = id(1200# integer object  
  4. val3 = id([25,336,95,236,92,3225]) # List object  
  5. # Displaying result  
  6. print(val)  
  7. print(val2)  
  8. print(val3)  

Output:

139963782059696
139963805666864
139963781994504

Python setattr() Function

Python setattr() function is used to set a value to the object's attribute. It takes three arguments, i.e., an object, a string, and an arbitrary value, and returns none. It is helpful when we want to add a new attribute to an object and set a value to it.

Python setattr() Function Example

  1. class Student:  
  2.     id = 0  
  3.     name = ""  
  4.       
  5.     def __init__(self, id, name):  
  6.         self.id = id  
  7.         self.name = name  
  8.           
  9. student = Student(102,"Sohan")  
  10. print(student.id)  
  11. print(student.name)  
  12. #print(student.email) product error  
  13. setattr(student, 'email','sohan@abc.com'# adding new attribute  
  14. print(student.email)  

Output:

102
Sohan
sohan@abc.com

Python slice() Function

Python slice() function is used to get a slice of elements from the collection of elements. Python provides two overloaded slice functions. The first function takes a single argument while the second function takes three arguments and returns a slice object. This slice object can be used to get a subsection of the collection.

Python slice() Function Example

  1. # Calling function  
  2. result = slice(5# returns slice object  
  3. result2 = slice(0,5,3# returns slice object  
  4. # Displaying result  
  5. print(result)  
  6. print(result2)  

Output:

slice(None, 5, None)
slice(0, 5, 3)

Python sorted() Function

Python sorted() function is used to sort elements. By default, it sorts elements in an ascending order but can be sorted in descending also. It takes four arguments and returns a collection in sorted order. In the case of a dictionary, it sorts only keys, not values.

Python sorted() Function Example

  1. str = "javatpoint" # declaring string  
  2. # Calling function  
  3. sorted1 = sorted(str) # sorting string  
  4. # Displaying result  
  5. print(sorted1)  

Output:

['a', 'a', 'i', 'j', 'n', 'o', 'p', 't', 't', 'v']

Python next() Function

Python next() function is used to fetch next item from the collection. It takes two arguments, i.e., an iterator and a default value, and returns an element.

This method calls on iterator and throws an error if no item is present. To avoid the error, we can set a default value.

Python next() Function Example

  1. number = iter([2563282]) # Creating iterator  
  2. # Calling function  
  3. item = next(number)   
  4. # Displaying result  
  5. print(item)  
  6. # second item  
  7. item = next(number)  
  8. print(item)  
  9. # third item  
  10. item = next(number)  
  11. print(item)  

Output:

256
32
82

Python input() Function

Python input() function is used to get an input from the user. It prompts for the user input and reads a line. After reading data, it converts it into a string and returns it. It throws an error EOFError if EOF is read.

Python input() Function Example

  1. # Calling function  
  2. val = input("Enter a value: ")  
  3. # Displaying result  
  4. print("You entered:",val)  

Output:

Enter a value: 45
You entered: 45

Python int() Function

Python int() function is used to get an integer value. It returns an expression converted into an integer number. If the argument is a floating-point, the conversion truncates the number. If the argument is outside the integer range, then it converts the number into a long type.

If the number is not a number or if a base is given, the number must be a string.

Python int() Function Example

  1. # Calling function  
  2. val = int(10# integer value  
  3. val2 = int(10.52# float value  
  4. val3 = int('10'# string value  
  5. # Displaying result  
  6. print("integer values :",val, val2, val3)  

Output:

integer values : 10 10 10

Python isinstance() Function

Python isinstance() function is used to check whether the given object is an instance of that class. If the object belongs to the class, it returns true. Otherwise returns False. It also returns true if the class is a subclass.

The isinstance() function takes two arguments, i.e., object and classinfo, and then it returns either True or False.

Python isinstance() function Example

  1. class Student:  
  2.     id = 101  
  3.     name = "John"  
  4.     def __init__(self, id, name):  
  5.         self.id=id  
  6.         self.name=name  
  7.   
  8. student = Student(1010,"John")  
  9. lst = [12,34,5,6,767]  
  10. # Calling function   
  11. print(isinstance(student, Student)) # isinstance of Student class  
  12. print(isinstance(lst, Student))  

Output:

True
False

Python oct() Function

Python oct() function is used to get an octal value of an integer number. This method takes an argument and returns an integer converted into an octal string. It throws an error TypeError, if argument type is other than an integer.

Python oct() function Example

  1. # Calling function  
  2. val = oct(10)  
  3. # Displaying result  
  4. print("Octal value of 10:",val)  

Output:

Octal value of 10: 0o12

Python ord() Function

The python ord() function returns an integer representing Unicode code point for the given Unicode character.

Python ord() function Example

  1. # Code point of an integer  
  2. print(ord('8'))  
  3.   
  4. # Code point of an alphabet   
  5. print(ord('R'))  
  6.   
  7. # Code point of a character  
  8. print(ord('&'))  

Output:

56
82
38

Python pow() Function

The python pow() function is used to compute the power of a number. It returns x to the power of y. If the third argument(z) is given, it returns x to the power of y modulus z, i.e. (x, y) % z.

Python pow() function Example

  1. # positive x, positive y (x**y)  
  2. print(pow(42))  
  3.   
  4. # negative x, positive y  
  5. print(pow(-42))  
  6.   
  7. # positive x, negative y (x**-y)  
  8. print(pow(4, -2))  
  9.   
  10. # negative x, negative y  
  11. print(pow(-4, -2))  

Output:

16
16
0.0625
0.0625

Python print() Function

The python print() function prints the given object to the screen or other standard output devices.

Python print() function Example

  1. print("Python is programming language.")  
  2.   
  3. x = 7  
  4. # Two objects passed  
  5. print("x =", x)  
  6.   
  7. y = x  
  8. # Three objects passed  
  9. print('x =', x, '= y')  

Output:

Python is programming language.
x = 7
x = 7 = y

Python range() Function

The python range() function returns an immutable sequence of numbers starting from 0 by default, increments by 1 (by default) and ends at a specified number.

Python range() function Example

  1. # empty range  
  2. print(list(range(0)))  
  3.   
  4. # using the range(stop)  
  5. print(list(range(4)))  
  6.   
  7. # using the range(start, stop)  
  8. print(list(range(1,7 )))  

Output:

[]
[0, 1, 2, 3]
[1, 2, 3, 4, 5, 6]

Python reversed() Function

The python reversed() function returns the reversed iterator of the given sequence.

Python reversed() function Example

  1. # for string  
  2. String = 'Java'  
  3. print(list(reversed(String)))  
  4.   
  5. # for tuple  
  6. Tuple = ('J''a''v''a')  
  7. print(list(reversed(Tuple)))  
  8.   
  9. # for range  
  10. Range = range(812)  
  11. print(list(reversed(Range)))  
  12.   
  13. # for list  
  14. List = [1275]  
  15. print(list(reversed(List)))  

Output:

['a', 'v', 'a', 'J']
['a', 'v', 'a', 'J']
[11, 10, 9, 8]
[5, 7, 2, 1]

Python round() Function

The python round() function rounds off the digits of a number and returns the floating point number.

Python round() Function Example

  1. #  for integers  
  2. print(round(10))  
  3.   
  4. #  for floating point  
  5. print(round(10.8))  
  6.   
  7. #  even choice  
  8. print(round(6.6))  

Output:

10
11
7

Python issubclass() Function

The python issubclass() function returns true if object argument(first argument) is a subclass of second class(second argument).

Python issubclass() Function Example

  1. class Rectangle:  
  2.   def __init__(rectangleType):  
  3.     print('Rectangle is a ', rectangleType)  
  4.   
  5. class Square(Rectangle):  
  6.   def __init__(self):  
  7.     Rectangle.__init__('square')  
  8.       
  9. print(issubclass(Square, Rectangle))  
  10. print(issubclass(Square, list))  
  11. print(issubclass(Square, (list, Rectangle)))  
  12. print(issubclass(Rectangle, (list, Rectangle)))  

Output:

True
False
True
True

Python str

The python str() converts a specified value into a string.

Python str() Function Example

  1. str('4')  

Output:

'4'

Python tuple() Function

The python tuple() function is used to create a tuple object.

Python tuple() Function Example

  1. t1 = tuple()  
  2. print('t1=', t1)  
  3.   
  4. # creating a tuple from a list  
  5. t2 = tuple([169])  
  6. print('t2=', t2)  
  7.   
  8. # creating a tuple from a string  
  9. t1 = tuple('Java')  
  10. print('t1=',t1)  
  11.   
  12. # creating a tuple from a dictionary  
  13. t1 = tuple({4'four'5'five'})  
  14. print('t1=',t1)  

Output:

t1= ()
t2= (1, 6, 9)
t1= ('J', 'a', 'v', 'a')
t1= (4, 5)

Python type()

The python type() returns the type of the specified object if a single argument is passed to the type() built in function. If three arguments are passed, then it returns a new type object.

Python type() Function Example

  1. List = [45]  
  2. print(type(List))  
  3.   
  4. Dict = {4'four'5'five'}  
  5. print(type(Dict))  
  6.   
  7. class Python:  
  8.     a = 0  
  9.   
  10. InstanceOfPython = Python()  
  11. print(type(InstanceOfPython))  

Output:

<class 'list'>
<class 'dict'>
<class '__main__.Python'>

Python vars() function

The python vars() function returns the __dict__ attribute of the given object.

Python vars() Function Example

  1. class Python:  
  2.   def __init__(self, x = 7, y = 9):  
  3.     self.x = x  
  4.     self.y = y  
  5.     
  6. InstanceOfPython = Python()  
  7. print(vars(InstanceOfPython))  

Output:

{'y': 9, 'x': 7}

Python zip() Function

The python zip() Function returns a zip object, which maps a similar index of multiple containers. It takes iterables (can be zero or more), makes it an iterator that aggregates the elements based on iterables passed, and returns an iterator of tuples.

Python zip() Function Example

  1. numList = [4,56]  
  2. strList = ['four''five''six']  
  3.   
  4. # No iterables are passed  
  5. result = zip()  
  6.   
  7. # Converting itertor to list  
  8. resultList = list(result)  
  9. print(resultList)  
  10.   
  11. # Two iterables are passed  
  12. result = zip(numList, strList)  
  13.   
  14. # Converting itertor to set  
  15. resultSet = set(result)  
  16. print(resultSet)  

Output:

[]
{(5, 'five'), (4, 'four'), (6, 'six')}






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Lambda Functions

Python Lambda function is known as the anonymous function that is defined without a name. Python allows us to not declare the function in the standard manner, i.e., by using the def keyword. Rather, the anonymous functions are declared by using the lambda keyword. However, Lambda functions can accept any number of arguments, but they can return only one value in the form of expression.

The anonymous function contains a small piece of code. It simulates inline functions of C and C++, but it is not exactly an inline function.

The syntax to define an anonymous function is given below.

Syntax

  1. lambda arguments: expression       

It can accept any number of arguments and has only one expression. It is useful when the function objects are required.

Consider the following example of the lambda function.

Example 1

  1. # a is an argument and a+10 is an expression which got evaluated and returned.    
  2. x = lambda a:a+10   
  3. # Here we are printing the function object  
  4. print(x)  
  5. print("sum = ",x(20))  

Output:

<function <lambda> at 0x0000019E285D16A8>
sum = 30

In the above example, we have defined the lambda a: a+10 anonymous function where a is an argument and a+10 is an expression. The given expression gets evaluated and returned the result. The above lambda function is same as the normal function.

  1. def x(a):  
  2.     return a+10  
  3. print(sum = x(10))  

Example 2

Multiple arguments to Lambda function

  1. # a and b are the arguments and a*b is the expression which gets evaluated and returned.    
  2. x = lambda a,b: a*b  
  3. print("mul = ", x(20,10))  
  4. <p><strong>Output:</strong></p>  
  5. <div class="codeblock3"><pre>  
  6. mul =  200  
  7. </pre></div>  
  8. <h2 class="h2">Why use lambda function?</h2>  
  9. <p>The main role of the lambda function is better described in the scenarios when we use them anonymously inside another function. In Python, the lambda function can be used as an argument to the <strong>higher-order functions</strong> which accepts other functions as arguments.</p>  
  10. <p>Consider the following example:</p>  
  11. <h3 class="h3">Example 1:</h3>  
  12. <div class="codeblock"><textarea name="code" class="python">  
  13. #the function table(n) prints the table of n    
  14. def table(n):    
  15.     return lambda a:a*n # a will contain the iteration variable i and a multiple of n is returned at each function call    
  16. n = int(input("Enter the number:"))    
  17. b = table(n) #the entered number is passed into the function table. b will contain a lambda function which is called again and again with the iteration variable i    
  18. for i in range(1,11):    
  19.     print(n,"X",i,"=",b(i)) #the lambda function b is called with the iteration variable i  

Output:

Enter the number:10
10 X 1 = 10
10 X 2 = 20
10 X 3 = 30
10 X 4 = 40
10 X 5 = 50
10 X 6 = 60
10 X 7 = 70
10 X 8 = 80
10 X 9 = 90
10 X 10 = 100

The lambda function is commonly used with Python built-in functions filter() function and map() function.

Use lambda function with filter()

The Python built-in filter() function accepts a function and a list as an argument. It provides an effective way to filter out all elements of the sequence. It returns the new sequence in which the function evaluates to True.

Consider the following example where we filter out the only odd number from the given list.

  1. #program to filter out the tuple which contains odd numbers    
  2. lst = (10,22,37,41,100,123,29)  
  3. oddlist = tuple(filter(lambda x:(x%3 == 0),lst)) # the tuple contains all the items of the tuple for which the lambda function evaluates to true    
  4. print(oddlist)    

Output:

(37, 41, 123, 29)

Using lambda function with map()

The map() function in Python accepts a function and a list. It gives a new list which contains all modified items returned by the function for each item.

Consider the following example of map() function.

  1. #program to filter out the list which contains odd numbers    
  2. lst = (10,20,30,40,50,60)  
  3. square_list = list(map(lambda x:x**2,lst)) # the tuple contains all the items of the list for which the lambda function evaluates to true    
  4. print(square_tuple)    

Output:

(100, 400, 900, 1600, 2500, 3600)

Next TopicPython Files Io





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python File Handling

Till now, we were taking the input from the console and writing it back to the console to interact with the user.

Sometimes, it is not enough to only display the data on the console. The data to be displayed may be very large, and only a limited amount of data can be displayed on the console since the memory is volatile, it is impossible to recover the programmatically generated data again and again.

The file handling plays an important role when the data needs to be stored permanently into the file. A file is a named location on disk to store related information. We can access the stored information (non-volatile) after the program termination.

The file-handling implementation is slightly lengthy or complicated in the other programming language, but it is easier and shorter in Python.

In Python, files are treated in two modes as text or binary. The file may be in the text or binary format, and each line of a file is ended with the special character.

Hence, a file operation can be done in the following order.

  • Open a file
  • Read or write - Performing operation
  • Close the file

Opening a file

Python provides an open() function that accepts two arguments, file name and access mode in which the file is accessed. The function returns a file object which can be used to perform various operations like reading, writing, etc.

Syntax:

  1. file object = open(<file-name>, <access-mode>, <buffering>)     

The files can be accessed using various modes like read, write, or append. The following are the details about the access mode to open a file.

SN Access mode Description
1 r It opens the file to read-only mode. The file pointer exists at the beginning. The file is by default open in this mode if no access mode is passed.
2 rb It opens the file to read-only in binary format. The file pointer exists at the beginning of the file.
3 r+ It opens the file to read and write both. The file pointer exists at the beginning of the file.
4 rb+ It opens the file to read and write both in binary format. The file pointer exists at the beginning of the file.
5 w It opens the file to write only. It overwrites the file if previously exists or creates a new one if no file exists with the same name. The file pointer exists at the beginning of the file.
6 wb It opens the file to write only in binary format. It overwrites the file if it exists previously or creates a new one if no file exists. The file pointer exists at the beginning of the file.
7 w+ It opens the file to write and read both. It is different from r+ in the sense that it overwrites the previous file if one exists whereas r+ doesn't overwrite the previously written file. It creates a new file if no file exists. The file pointer exists at the beginning of the file.
8 wb+ It opens the file to write and read both in binary format. The file pointer exists at the beginning of the file.
9 a It opens the file in the append mode. The file pointer exists at the end of the previously written file if exists any. It creates a new file if no file exists with the same name.
10 ab It opens the file in the append mode in binary format. The pointer exists at the end of the previously written file. It creates a new file in binary format if no file exists with the same name.
11 a+ It opens a file to append and read both. The file pointer remains at the end of the file if a file exists. It creates a new file if no file exists with the same name.
12 ab+ It opens a file to append and read both in binary format. The file pointer remains at the end of the file.

Let's look at the simple example to open a file named "file.txt" (stored in the same directory) in read mode and printing its content on the console.

Example

  1. #opens the file file.txt in read mode    
  2. fileptr = open("file.txt","r")    
  3.     
  4. if fileptr:    
  5.     print("file is opened successfully")    

Output:

<class '_io.TextIOWrapper'>
file is opened successfully

In the above code, we have passed filename as a first argument and opened file in read mode as we mentioned r as the second argument. The fileptr holds the file object and if the file is opened successfully, it will execute the print statement

The close() method

Once all the operations are done on the file, we must close it through our Python script using the close() method. Any unwritten information gets destroyed once the close() method is called on a file object.

We can perform any operation on the file externally using the file system which is the currently opened in Python; hence it is good practice to close the file once all the operations are done.

The syntax to use the close() method is given below.

Syntax

  1. fileobject.close()   

Consider the following example.

  1. # opens the file file.txt in read mode    
  2. fileptr = open("file.txt","r")    
  3.     
  4. if fileptr:    
  5.     print("file is opened successfully")    
  6.     
  7. #closes the opened file    
  8. fileptr.close()  

After closing the file, we cannot perform any operation in the file. The file needs to be properly closed. If any exception occurs while performing some operations in the file then the program terminates without closing the file.

We should use the following method to overcome such type of problem.

  1. try:  
  2.    fileptr = open("file.txt")  
  3.    # perform file operations  
  4. finally:  
  5.    fileptr.close()  

The with statement

The with statement was introduced in python 2.5. The with statement is useful in the case of manipulating the files. It is used in the scenario where a pair of statements is to be executed with a block of code in between.

The syntax to open a file using with the statement is given below.

  1. with open(<file name>, <access mode>) as <file-pointer>:    
  2.     #statement suite     

The advantage of using with statement is that it provides the guarantee to close the file regardless of how the nested block exits.

It is always suggestible to use the with statement in the case of files because, if the break, return, or exception occurs in the nested block of code then it automatically closes the file, we don't need to write the close() function. It doesn't let the file to corrupt.

Consider the following example.

Example

  1. with open("file.txt",'r') as f:    
  2.     content = f.read();    
  3.     print(content)    

Writing the file

To write some text to a file, we need to open the file using the open method with one of the following access modes.

w: It will overwrite the file if any file exists. The file pointer is at the beginning of the file.

a: It will append the existing file. The file pointer is at the end of the file. It creates a new file if no file exists.

Consider the following example.

Example

  1. # open the file.txt in append mode. Create a new file if no such file exists.  
  2. fileptr = open("file2.txt""w")  
  3.   
  4. # appending the content to the file  
  5. fileptr.write('''''Python is the modern day language. It makes things so simple. 
  6. It is the fastest-growing programing language''')  
  7.   
  8. # closing the opened the file  
  9. fileptr.close()  

Output:

File2.txt

Python is the modern-day language. It makes things so simple. It is the fastest growing programming language.

Snapshot of the file2.txt

Python File Handling

We have opened the file in w mode. The file1.txt file doesn't exist, it created a new file and we have written the content in the file using the write() function.

Example 2

  1. #open the file.txt in write mode.    
  2. fileptr = open("file2.txt","a")  
  3.     
  4. #overwriting the content of the file    
  5. fileptr.write(" Python has an easy syntax and user-friendly interaction.")    
  6.       
  7. #closing the opened file     
  8. fileptr.close()  

Output:

Python is the modern day language. It makes things so simple.
It is the fastest growing programing language Python has an easy syntax and user-friendly interaction.

Snapshot of the file2.txt

Python File Handling

We can see that the content of the file is modified. We have opened the file in a mode and it appended the content in the existing file2.txt.

To read a file using the Python script, the Python provides the read() method. The read() method reads a string from the file. It can read the data in the text as well as a binary format.

The syntax of the read() method is given below.

Syntax:

  1. fileobj.read(<count>)    

Here, the count is the number of bytes to be read from the file starting from the beginning of the file. If the count is not specified, then it may read the content of the file until the end.

Consider the following example.

Example

  1. #open the file.txt in read mode. causes error if no such file exists.    
  2. fileptr = open("file2.txt","r")  
  3. #stores all the data of the file into the variable content    
  4. content = fileptr.read(10)   
  5. # prints the type of the data stored in the file    
  6. print(type(content))      
  7. #prints the content of the file    
  8. print(content)       
  9. #closes the opened file    
  10. fileptr.close()    

Output:

<class 'str'>
Python is

In the above code, we have read the content of file2.txt by using the read() function. We have passed count value as ten which means it will read the first ten characters from the file.

If we use the following line, then it will print all content of the file.

  1. content = fileptr.read()  
  2. print(content)   

Output:

Python is the modern-day language. It makes things so simple.
It is the fastest-growing programing language Python has easy an syntax and user-friendly interaction.

Read file through for loop

We can read the file using for loop. Consider the following example.

  1. #open the file.txt in read mode. causes an error if no such file exists.    
  2. fileptr = open("file2.txt","r");     
  3. #running a for loop     
  4. for i in fileptr:    
  5.     print(i) # i contains each line of the file     

Output:

Python is the modern day language.

It makes things so simple.

Python has easy syntax and user-friendly interaction.

Read Lines of the file

Python facilitates to read the file line by line by using a function readline() method. The readline() method reads the lines of the file from the beginning, i.e., if we use the readline() method two times, then we can get the first two lines of the file.

Consider the following example which contains a function readline() that reads the first line of our file "file2.txt" containing three lines. Consider the following example.

Example 1: Reading lines using readline() function

  1. #open the file.txt in read mode. causes error if no such file exists.    
  2. fileptr = open("file2.txt","r");     
  3. #stores all the data of the file into the variable content    
  4. content = fileptr.readline()     
  5. content1 = fileptr.readline()  
  6. #prints the content of the file    
  7. print(content)     
  8. print(content1)  
  9. #closes the opened file    
  10. fileptr.close()    

Output:

Python is the modern day language.

It makes things so simple.

We called the readline() function two times that's why it read two lines from the file.

Python provides also the readlines() method which is used for the reading lines. It returns the list of the lines till the end of file(EOF) is reached.

Example 2: Reading Lines Using readlines() function

  1. #open the file.txt in read mode. causes error if no such file exists.    
  2. fileptr = open("file2.txt","r");     
  3.     
  4. #stores all the data of the file into the variable content    
  5. content = fileptr.readlines()     
  6.   
  7. #prints the content of the file    
  8. print(content)     
  9.     
  10. #closes the opened file    
  11. fileptr.close()    

Output:

['Python is the modern day language.\n', 'It makes things so simple.\n', 'Python has easy syntax and user-friendly interaction.']

Creating a new file

The new file can be created by using one of the following access modes with the function open().

x: it creates a new file with the specified name. It causes an error a file exists with the same name.

a: It creates a new file with the specified name if no such file exists. It appends the content to the file if the file already exists with the specified name.

w: It creates a new file with the specified name if no such file exists. It overwrites the existing file.

Consider the following example.

Example 1

  1. #open the file.txt in read mode. causes error if no such file exists.    
  2. fileptr = open("file2.txt","x")   
  3. print(fileptr)    
  4. if fileptr:    
  5.     print("File created successfully")  

Output:

<_io.TextIOWrapper name='file2.txt' mode='x' encoding='cp1252'>
File created successfully

File Pointer positions

Python provides the tell() method which is used to print the byte number at which the file pointer currently exists. Consider the following example.

  1. # open the file file2.txt in read mode    
  2. fileptr = open("file2.txt","r")    
  3.   
  4. #initially the filepointer is at 0     
  5. print("The filepointer is at byte :",fileptr.tell())    
  6.     
  7. #reading the content of the file    
  8. content = fileptr.read();    
  9.     
  10. #after the read operation file pointer modifies. tell() returns the location of the fileptr.     
  11.     
  12. print("After reading, the filepointer is at:",fileptr.tell())    

Output:

The filepointer is at byte : 0
After reading, the filepointer is at: 117

Modifying file pointer position

In real-world applications, sometimes we need to change the file pointer location externally since we may need to read or write the content at various locations.

For this purpose, the Python provides us the seek() method which enables us to modify the file pointer position externally.

The syntax to use the seek() method is given below.

Syntax:

  1. <file-ptr>.seek(offset[, from)    

The seek() method accepts two parameters:

offset: It refers to the new position of the file pointer within the file.

from: It indicates the reference position from where the bytes are to be moved. If it is set to 0, the beginning of the file is used as the reference position. If it is set to 1, the current position of the file pointer is used as the reference position. If it is set to 2, the end of the file pointer is used as the reference position.

Consider the following example.

Example

  1. # open the file file2.txt in read mode    
  2. fileptr = open("file2.txt","r")    
  3.     
  4. #initially the filepointer is at 0     
  5. print("The filepointer is at byte :",fileptr.tell())    
  6.     
  7. #changing the file pointer location to 10.    
  8. fileptr.seek(10);    
  9.     
  10. #tell() returns the location of the fileptr.     
  11. print("After reading, the filepointer is at:",fileptr.tell())    

Output:

The filepointer is at byte : 0
After reading, the filepointer is at: 10

Python OS module

Renaming the file

The Python os module enables interaction with the operating system. The os module provides the functions that are involved in file processing operations like renaming, deleting, etc. It provides us the rename() method to rename the specified file to a new name. The syntax to use the rename() method is given below.

Syntax:

  1. rename(current-name, new-name)    

The first argument is the current file name and the second argument is the modified name. We can change the file name bypassing these two arguments.

Example 1:

  1. import os    
  2.     
  3. #rename file2.txt to file3.txt    
  4. os.rename("file2.txt","file3.txt")  

Output:

The above code renamed current file2.txt to file3.txt

Removing the file

The os module provides the remove() method which is used to remove the specified file. The syntax to use the remove() method is given below.

  1. remove(file-name)   

Example 1

  1. import os;    
  2. #deleting the file named file3.txt     
  3. os.remove("file3.txt")    

Creating the new directory

The mkdir() method is used to create the directories in the current working directory. The syntax to create the new directory is given below.

Syntax:

  1. mkdir(directory name)  

Example 1

  1. import os    
  2.     
  3. #creating a new directory with the name new    
  4. os.mkdir("new")    

The getcwd() method

This method returns the current working directory.

The syntax to use the getcwd() method is given below.

Syntax

  1. os.getcwd()  

Example

  1. import os  
  2. os.getcwd()  

Output:

'C:\\Users\\DEVANSH SHARMA'

Changing the current working directory

The chdir() method is used to change the current working directory to a specified directory.

The syntax to use the chdir() method is given below.

Syntax

  1. chdir("new-directory")    

Example

  1. import os   
  2. # Changing current directory with the new directiory  
  3. os.chdir("C:\\Users\\DEVANSH SHARMA\\Documents")  
  4. #It will display the current working directory  
  5. os.getcwd()  

Output:

'C:\\Users\\DEVANSH SHARMA\\Documents'

Deleting directory

The rmdir() method is used to delete the specified directory.

The syntax to use the rmdir() method is given below.

Syntax

  1. os.rmdir(directory name)    

Example 1

  1. import os  
  2. #removing the new directory     
  3. os.rmdir("directory_name")    

It will remove the specified directory.

Writing Python output to the files

In Python, there are the requirements to write the output of a Python script to a file.

The check_call() method of module subprocess is used to execute a Python script and write the output of that script to a file.

The following example contains two python scripts. The script file1.py executes the script file.py and writes its output to the text file output.txt.

Example

file.py

  1. temperatures=[10,-20,-289,100]    
  2. def c_to_f(c):    
  3.     if c< -273.15:    
  4.         return "That temperature doesn't make sense!"    
  5.     else:    
  6.         f=c*9/5+32    
  7.         return f    
  8. for t in temperatures:    
  9.     print(c_to_f(t))    

file.py

  1. import subprocess    
  2.     
  3. with open("output.txt""wb") as f:    
  4.     subprocess.check_call(["python""file.py"], stdout=f)    

The file related methods

The file object provides the following methods to manipulate the files on various operating systems.

SN Method Description
1 file.close() It closes the opened file. The file once closed, it can't be read or write anymore.
2 File.fush() It flushes the internal buffer.
3 File.fileno() It returns the file descriptor used by the underlying implementation to request I/O from the OS.
4 File.isatty() It returns true if the file is connected to a TTY device, otherwise returns false.
5 File.next() It returns the next line from the file.
6 File.read([size]) It reads the file for the specified size.
7 File.readline([size]) It reads one line from the file and places the file pointer to the beginning of the new line.
8 File.readlines([sizehint]) It returns a list containing all the lines of the file. It reads the file until the EOF occurs using readline() function.
9 File.seek(offset[,from) It modifies the position of the file pointer to a specified offset with the specified reference.
10 File.tell() It returns the current position of the file pointer within the file.
11 File.truncate([size]) It truncates the file to the optional specified size.
12 File.write(str) It writes the specified string to a file
13 File.writelines(seq) It writes a sequence of the strings to a file.

Next TopicPython Modules





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Modules

A python module can be defined as a python program file which contains a python code including python functions, class, or variables. In other words, we can say that our python code file saved with the extension (.py) is treated as the module. We may have a runnable code inside the python module.

Modules in Python provides us the flexibility to organize the code in a logical way.

To use the functionality of one module into another, we must have to import the specific module.

Example

In this example, we will create a module named as file.py which contains a function func that contains a code to print some message on the console.

Let's create the module named as file.py.

  1. #displayMsg prints a message to the name being passed.   
  2. def displayMsg(name)  
  3.     print("Hi "+name);    

Here, we need to include this module into our main module to call the method displayMsg() defined in the module named file.

Loading the module in our python code

We need to load the module in our python code to use its functionality. Python provides two types of statements as defined below.

  1. The import statement
  2. The from-import statement

The import statement

The import statement is used to import all the functionality of one module into another. Here, we must notice that we can use the functionality of any python source file by importing that file as the module into another python source file.

We can import multiple modules with a single import statement, but a module is loaded once regardless of the number of times, it has been imported into our file.

The syntax to use the import statement is given below.

  1. import module1,module2,........ module n  

Hence, if we need to call the function displayMsg() defined in the file file.py, we have to import that file as a module into our module as shown in the example below.

Example:

  1. import file;  
  2. name = input("Enter the name?")  
  3. file.displayMsg(name)  

Output:

Enter the name?John
Hi John

The from-import statement

Instead of importing the whole module into the namespace, python provides the flexibility to import only the specific attributes of a module. This can be done by using from? import statement. The syntax to use the from-import statement is given below.

  1. from < module-name> import <name 1>, <name 2>..,<name n>   

Consider the following module named as calculation which contains three functions as summation, multiplication, and divide.

calculation.py:

  1. #place the code in the calculation.py   
  2. def summation(a,b):  
  3.     return a+b  
  4. def multiplication(a,b):  
  5.     return a*b;  
  6. def divide(a,b):  
  7.     return a/b;  

Main.py:

  1. from calculation import summation    
  2. #it will import only the summation() from calculation.py  
  3. a = int(input("Enter the first number"))  
  4. b = int(input("Enter the second number"))  
  5. print("Sum = ",summation(a,b)) #we do not need to specify the module name while accessing summation()  

Output:

Enter the first number10
Enter the second number20
Sum = 30

The from...import statement is always better to use if we know the attributes to be imported from the module in advance. It doesn't let our code to be heavier. We can also import all the attributes from a module by using *.

Consider the following syntax.

  1. from <module> import *   

Renaming a module

Python provides us the flexibility to import some module with a specific name so that we can use this name to use that module in our python source file.

The syntax to rename a module is given below.

  1. import <module-name> as <specific-name>   

Example

  1. #the module calculation of previous example is imported in this example as cal.   
  2. import calculation as cal;  
  3. a = int(input("Enter a?"));  
  4. b = int(input("Enter b?"));  
  5. print("Sum = ",cal.summation(a,b))  

Output:

Enter a?10
Enter b?20
Sum = 30

Using dir() function

The dir() function returns a sorted list of names defined in the passed module. This list contains all the sub-modules, variables and functions defined in this module.

Consider the following example.

Example

  1. import json  
  2.   
  3. List = dir(json)  
  4.   
  5. print(List)  

Output:

['JSONDecoder', 'JSONEncoder', '__all__', '__author__', '__builtins__', '__cached__', '__doc__',
'__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__',
'_default_decoder', '_default_encoder', 'decoder', 'dump', 'dumps', 'encoder', 'load', 'loads', 'scanner']

The reload() function

As we have already stated that, a module is loaded once regardless of the number of times it is imported into the python source file. However, if you want to reload the already imported module to re-execute the top-level code, python provides us the reload() function. The syntax to use the reload() function is given below.

  1. reload(<module-name>)  

for example, to reload the module calculation defined in the previous example, we must use the following line of code.

  1. reload(calculation)  

Scope of variables

In Python, variables are associated with two types of scopes. All the variables defined in a module contain the global scope unless or until it is defined within a function.

All the variables defined inside a function contain a local scope that is limited to this function itself. We can not access a local variable globally.

If two variables are defined with the same name with the two different scopes, i.e., local and global, then the priority will always be given to the local variable.

Consider the following example.

Example

  1. name = "john"  
  2. def print_name(name):  
  3.     print("Hi",name) #prints the name that is local to this function only.  
  4. name = input("Enter the name?")  
  5. print_name(name)  

Output:

Hi David

Python packages

The packages in python facilitate the developer with the application development environment by providing a hierarchical directory structure where a package contains sub-packages, modules, and sub-modules. The packages are used to categorize the application level code efficiently.

Let's create a package named Employees in your home directory. Consider the following steps.

1. Create a directory with name Employees on path /home.

2. Create a python source file with name ITEmployees.py on the path /home/Employees.

ITEmployees.py

  1. def getITNames():  
  2.     List = ["John""David""Nick",    "Martin"]  
  3.     return List;  

3. Similarly, create one more python file with name BPOEmployees.py and create a function getBPONames().

4. Now, the directory Employees which we have created in the first step contains two python modules. To make this directory a package, we need to include one more file here, that is __init__.py which contains the import statements of the modules defined in this directory.

__init__.py

  1. from ITEmployees import getITNames  
  2. from BPOEmployees import getBPONames  

5. Now, the directory Employees has become the package containing two python modules. Here we must notice that we must have to create __init__.py inside a directory to convert this directory to a package.

6. To use the modules defined inside the package Employees, we must have to import this in our python source file. Let's create a simple python source file at our home directory (/home) which uses the modules defined in this package.

Test.py

  1. import Employees  
  2. print(Employees.getNames())  

Output:

['John', 'David', 'Nick', 'Martin']

We can have sub-packages inside the packages. We can nest the packages up to any level depending upon the application requirements.

The following image shows the directory structure of an application Library management system which contains three sub-packages as Admin, Librarian, and Student. The sub-packages contain the python modules.

Python packages




Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Exception

An exception can be defined as an unusual condition in a program resulting in the interruption in the flow of the program.

Whenever an exception occurs, the program stops the execution, and thus the further code is not executed. Therefore, an exception is the run-time errors that are unable to handle to Python script. An exception is a Python object that represents an error

Python provides a way to handle the exception so that the code can be executed without any interruption. If we do not handle the exception, the interpreter doesn't execute all the code that exists after the exception.

Python has many built-in exceptions that enable our program to run without interruption and give the output. These exceptions are given below:

Common Exceptions

Python provides the number of built-in exceptions, but here we are describing the common standard exceptions. A list of common exceptions that can be thrown from a standard Python program is given below.

  1. ZeroDivisionError: Occurs when a number is divided by zero.
  2. NameError: It occurs when a name is not found. It may be local or global.
  3. IndentationError: If incorrect indentation is given.
  4. IOError: It occurs when Input Output operation fails.
  5. EOFError: It occurs when the end of the file is reached, and yet operations are being performed.

The problem without handling exceptions

As we have already discussed, the exception is an abnormal condition that halts the execution of the program.

Suppose we have two variables a and b, which take the input from the user and perform the division of these values. What if the user entered the zero as the denominator? It will interrupt the program execution and through a ZeroDivision exception. Let's see the following example.

Example

  1. a = int(input("Enter a:"))    
  2. b = int(input("Enter b:"))    
  3. c = a/b  
  4. print("a/b = %d" %c)    
  5.     
  6. #other code:    
  7. print("Hi I am other part of the program")  

Output:

Enter a:10
Enter b:0
Traceback (most recent call last):
File "exception-test.py", line 3, in <module>
c = a/b;
ZeroDivisionError: division by zero

The above program is syntactically correct, but it through the error because of unusual input. That kind of programming may not be suitable or recommended for the projects because these projects are required uninterrupted execution. That's why an exception-handling plays an essential role in handling these unexpected exceptions. We can handle these exceptions in the following way.

Exception handling in python

The try-expect statement

If the Python program contains suspicious code that may throw the exception, we must place that code in the try block. The try block must be followed with the except statement, which contains a block of code that will be executed if there is some exception in the try block.

Python Exception handling

Syntax

  1. try:    
  2.     #block of code     
  3.     
  4. except Exception1:    
  5.     #block of code    
  6.     
  7. except Exception2:    
  8.     #block of code    
  9.     
  10. #other code    

Consider the following example.

Example 1

  1. try:  
  2.     a = int(input("Enter a:"))    
  3.     b = int(input("Enter b:"))    
  4.     c = a/b  
  5. except:  
  6.     print("Can't divide with zero")  

Output:

Enter a:10
Enter b:0
Can't divide with zero

We can also use the else statement with the try-except statement in which, we can place the code which will be executed in the scenario if no exception occurs in the try block.

The syntax to use the else statement with the try-except statement is given below.

  1. try:    
  2.     #block of code     
  3.     
  4. except Exception1:    
  5.     #block of code     
  6.     
  7. else:    
  8.     #this code executes if no except block is executed    

Python Exception handling

Consider the following program.

Example 2

  1. try:    
  2.     a = int(input("Enter a:"))    
  3.     b = int(input("Enter b:"))    
  4.     c = a/b  
  5.     print("a/b = %d"%c)    
  6. # Using Exception with except statement. If we print(Exception) it will return exception class  
  7. except Exception:    
  8.     print("can't divide by zero")    
  9.     print(Exception)  
  10. else:    
  11.     print("Hi I am else block")     

Output:

Enter a:10
Enter b:0
can't divide by zero
<class 'Exception'>

The except statement with no exception

Python provides the flexibility not to specify the name of exception with the exception statement.

Consider the following example.

Example

  1. try:    
  2.     a = int(input("Enter a:"))    
  3.     b = int(input("Enter b:"))    
  4.     c = a/b;    
  5.     print("a/b = %d"%c)    
  6. except:    
  7.     print("can't divide by zero")    
  8. else:    
  9.     print("Hi I am else block")     

The except statement using with exception variable

We can use the exception variable with the except statement. It is used by using the as keyword. this object will return the cause of the exception. Consider the following example:

  1. try:    
  2.     a = int(input("Enter a:"))    
  3.     b = int(input("Enter b:"))    
  4.     c = a/b  
  5.     print("a/b = %d"%c)    
  6.     # Using exception object with the except statement  
  7. except Exception as e:    
  8.     print("can't divide by zero")    
  9.     print(e)  
  10. else:    
  11.     print("Hi I am else block")     

Output:

Enter a:10
Enter b:0
can't divide by zero
division by zero

Points to remember

  1. Python facilitates us to not specify the exception with the except statement.
  2. We can declare multiple exceptions in the except statement since the try block may contain the statements which throw the different type of exceptions.
  3. We can also specify an else block along with the try-except statement, which will be executed if no exception is raised in the try block.
  4. The statements that don't throw the exception should be placed inside the else block.

Example

  1. try:    
  2.     #this will throw an exception if the file doesn't exist.     
  3.     fileptr = open("file.txt","r")    
  4. except IOError:    
  5.     print("File not found")    
  6. else:    
  7.     print("The file opened successfully")    
  8.     fileptr.close()    

Output:

File not found

Declaring Multiple Exceptions

The Python allows us to declare the multiple exceptions with the except clause. Declaring multiple exceptions is useful in the cases where a try block throws multiple exceptions. The syntax is given below.

Syntax

  1. try:    
  2.     #block of code     
  3.     
  4. except (<Exception 1>,<Exception 2>,<Exception 3>,...<Exception n>)    
  5.     #block of code     
  6.     
  7. else:    
  8.     #block of code    

Consider the following example.

  1. try:      
  2.     a=10/0;      
  3. except(ArithmeticError, IOError):      
  4.     print("Arithmetic Exception")      
  5. else:      
  6.     print("Successfully Done")       

Output:

Arithmetic Exception

The try...finally block

Python provides the optional finally statement, which is used with the try statement. It is executed no matter what exception occurs and used to release the external resource. The finally block provides a guarantee of the execution.

We can use the finally block with the try block in which we can pace the necessary code, which must be executed before the try statement throws an exception.

The syntax to use the finally block is given below.

Syntax

  1. try:    
  2.     # block of code     
  3.     # this may throw an exception    
  4. finally:    
  5.     # block of code    
  6.     # this will always be executed     

Python Exception handling

Example

  1. try:    
  2.     fileptr = open("file2.txt","r")      
  3.     try:    
  4.         fileptr.write("Hi I am good")    
  5.     finally:    
  6.         fileptr.close()    
  7.         print("file closed")    
  8. except:    
  9.     print("Error")    

Output:

file closed
Error

Raising exceptions

An exception can be raised forcefully by using the raise clause in Python. It is useful in in that scenario where we need to raise an exception to stop the execution of the program.

For example, there is a program that requires 2GB memory for execution, and if the program tries to occupy 2GB of memory, then we can raise an exception to stop the execution of the program.

The syntax to use the raise statement is given below.

Syntax

  1. raise Exception_class,<value>    

Points to remember

  1. To raise an exception, the raise statement is used. The exception class name follows it.
  2. An exception can be provided with a value that can be given in the parenthesis.
  3. To access the value "as" keyword is used. "e" is used as a reference variable which stores the value of the exception.
  4. We can pass the value to an exception to specify the exception type.

Example

  1. try:    
  2.     age = int(input("Enter the age:"))    
  3.     if(age<18):    
  4.         raise ValueError   
  5.     else:    
  6.         print("the age is valid")    
  7. except ValueError:    
  8.     print("The age is not valid")    

Output:

Enter the age:17
The age is not valid

Example 2 Raise the exception with message

  1. try:  
  2.      num = int(input("Enter a positive integer: "))  
  3.      if(num <= 0):  
  4. # we can pass the message in the raise statement  
  5.          raise ValueError("That is  a negative number!")  
  6. except ValueError as e:  
  7.      print(e)  

Output:

Enter a positive integer: -5
That is a negative number!

Example 3

  1. try:    
  2.     a = int(input("Enter a:"))    
  3.     b = int(input("Enter b:"))    
  4.     if b is 0:    
  5.         raise ArithmeticError  
  6.     else:    
  7.         print("a/b = ",a/b)    
  8. except ArithmeticError:    
  9.     print("The value of b can't be 0")  

Output:

Enter a:10
Enter b:0
The value of b can't be 0

Custom Exception

The Python allows us to create our exceptions that can be raised from the program and caught using the except clause. However, we suggest you read this section after visiting the Python object and classes.

Consider the following example.

Example

  1. class ErrorInCode(Exception):      
  2.     def __init__(self, data):      
  3.         self.data = data      
  4.     def __str__(self):      
  5.         return repr(self.data)      
  6.       
  7. try:      
  8.     raise ErrorInCode(2000)      
  9. except ErrorInCode as ae:      
  10.     print("Received error:", ae.data)      

Output:

Received error: 2000






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Date and time

Python provides the datetime module work with real dates and times. In real-world applications, we need to work with the date and time. Python enables us to schedule our Python script to run at a particular timing.

In Python, the date is not a data type, but we can work with the date objects by importing the module named with datetime, time, and calendar.

In this section of the tutorial, we will discuss how to work with the date and time objects in Python.

The datetime classes are classified in the six main classes.

  • date - It is a naive ideal date. It consists of the year, month, and day as attributes.
  • time - It is a perfect time, assuming every day has precisely 24*60*60 seconds. It has hour, minute, second, microsecond, and tzinfo as attributes.
  • datetime - It is a grouping of date and time, along with the attributes year, month, day, hour, minute, second, microsecond, and tzinfo.
  • timedelta - It represents the difference between two dates, time or datetime instances to microsecond resolution.
  • tzinfo - It provides time zone information objects.
  • timezone - It is included in the new version of Python. It is the class that implements the tzinfo abstract base class.

Tick

In Python, the time instants are counted since 12 AM, 1st January 1970. The function time() of the module time returns the total number of ticks spent since 12 AM, 1st January 1970. A tick can be seen as the smallest unit to measure the time.

Consider the following example

  1. import time;  
  2. #prints the number of ticks spent since 12 AM, 1st January 1970  
  3. print(time.time())  

Output:

1585928913.6519969

How to get the current time?

The localtime() functions of the time module are used to get the current time tuple. Consider the following example.

Example

  1. import time;    
  2.     
  3. #returns a time tuple     
  4.     
  5. print(time.localtime(time.time()))  

Output:

time.struct_time(tm_year=2020, tm_mon=4, tm_mday=3, tm_hour=21, tm_min=21, tm_sec=40, tm_wday=4, tm_yday=94, tm_isdst=0)

Time tuple

The time is treated as the tuple of 9 numbers. Let's look at the members of the time tuple.

Index Attribute Values
0 Year 4 digit (for example 2018)
1 Month 1 to 12
2 Day 1 to 31
3 Hour 0 to 23
4 Minute 0 to 59
5 Second 0 to 60
6 Day of weak 0 to 6
7 Day of year 1 to 366
8 Daylight savings -1, 0, 1 , or -1

Getting formatted time

The time can be formatted by using the asctime() function of the time module. It returns the formatted time for the time tuple being passed.

Example

  1. import time    
  2.   #returns the formatted time      
  3.   
  4. print(time.asctime(time.localtime(time.time())))  

Output:

Tue Dec 18 15:31:39 2018

Python sleep time

The sleep() method of time module is used to stop the execution of the script for a given amount of time. The output will be delayed for the number of seconds provided as the float.

Consider the following example.

Example

  1. import time  
  2. for i in range(0,5):  
  3.     print(i)  
  4.     #Each element will be printed after 1 second  
  5.     time.sleep(1)  

Output:

0
1
2
3
4

The datetime Module

The datetime module enables us to create the custom date objects, perform various operations on dates like the comparison, etc.

To work with dates as date objects, we have to import the datetime module into the python source code.

Consider the following example to get the datetime object representation for the current time.

Example

  1. import datetime  
  2. #returns the current datetime object     
  3. print(datetime.datetime.now())    

Output:

2020-04-04 13:18:35.252578

Creating date objects

We can create the date objects bypassing the desired date in the datetime constructor for which the date objects are to be created.

Consider the following example.

Example

  1. import datetime    
  2. #returns the datetime object for the specified date    
  3. print(datetime.datetime(2020,04,04))    

Output:

2020-04-04 00:00:00

We can also specify the time along with the date to create the datetime object. Consider the following example.

Example

  1. import datetime  
  2.     
  3. #returns the datetime object for the specified time      
  4.     
  5. print(datetime.datetime(2020,4,4,1,26,40))    

Output:

2020-04-04 01:26:40

In the above code, we have passed in datetime() function year, month, day, hour, minute, and millisecond attributes in a sequential manner.

Comparison of two dates

We can compare two dates by using the comparison operators like >, >=, <, and <=.

Consider the following example.

Example

  1. from datetime import datetime as dt    
  2. #Compares the time. If the time is in between 8AM and 4PM, then it prints working hours otherwise it prints fun hours    
  3. if dt(dt.now().year,dt.now().month,dt.now().day,8)<dt.now()<dt(dt.now().year,dt.now().month,dt.now().day,16):    
  4.     print("Working hours....")    
  5. else:    
  6.     print("fun hours")   

Output:

fun hours

The calendar module

Python provides a calendar object that contains various methods to work with the calendars.

Consider the following example to print the calendar for the last month of 2018.

Example

  1. import calendar;    
  2. cal = calendar.month(2020,3)    
  3. #printing the calendar of December 2018    
  4. print(cal)    

Output:

March 2020
Mo Tu We Th Fr Sa Su
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30 31

Printing the calendar of whole year

The prcal() method of calendar module is used to print the calendar of the entire year. The year of which the calendar is to be printed must be passed into this method.

Example

  1. import calendar    
  2. #printing the calendar of the year 2019    
  3. s = calendar.prcal(2020)  

Output:

Python Date and time





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Regular Expressions

The regular expressions can be defined as the sequence of characters which are used to search for a pattern in a string. The module re provides the support to use regex in the python program. The re module throws an exception if there is some error while using the regular expression.

The re module must be imported to use the regex functionalities in python.

  1. import re   

Regex Functions

The following regex functions are used in the python.

SN Function Description
1 match This method matches the regex pattern in the string with the optional flag. It returns true if a match is found in the string otherwise it returns false.
2 search This method returns the match object if there is a match found in the string.
3 findall It returns a list that contains all the matches of a pattern in the string.
4 split Returns a list in which the string has been split in each match.
5 sub Replace one or many matches in the string.

Forming a regular expression

A regular expression can be formed by using the mix of meta-characters, special sequences, and sets.

Meta-Characters

Metacharacter is a character with the specified meaning.

Metacharacter Description Example
[] It represents the set of characters. "[a-z]"
\ It represents the special sequence. "\r"
. It signals that any character is present at some specific place. "Ja.v."
^ It represents the pattern present at the beginning of the string. "^Java"
$ It represents the pattern present at the end of the string. "point"
* It represents zero or more occurrences of a pattern in the string. "hello*"
+ It represents one or more occurrences of a pattern in the string. "hello+"
{} The specified number of occurrences of a pattern the string. "java{2}"
| It represents either this or that character is present. "java|point"
() Capture and group

Special Sequences

Special sequences are the sequences containing \ followed by one of the characters.

Character Description
\A It returns a match if the specified characters are present at the beginning of the string.
\b It returns a match if the specified characters are present at the beginning or the end of the string.
\B It returns a match if the specified characters are present at the beginning of the string but not at the end.
\d It returns a match if the string contains digits [0-9].
\D It returns a match if the string doesn't contain the digits [0-9].
\s It returns a match if the string contains any white space character.
\S It returns a match if the string doesn't contain any white space character.
\w It returns a match if the string contains any word characters.
\W It returns a match if the string doesn't contain any word.
\Z Returns a match if the specified characters are at the end of the string.

Sets

A set is a group of characters given inside a pair of square brackets. It represents the special meaning.

SN Set Description
1 [arn] Returns a match if the string contains any of the specified characters in the set.
2 [a-n] Returns a match if the string contains any of the characters between a to n.
3 [^arn] Returns a match if the string contains the characters except a, r, and n.
4 [0123] Returns a match if the string contains any of the specified digits.
5 [0-9] Returns a match if the string contains any digit between 0 and 9.
6 [0-5][0-9] Returns a match if the string contains any digit between 00 and 59.
10 [a-zA-Z] Returns a match if the string contains any alphabet (lower-case or upper-case).

The findall() function

This method returns a list containing a list of all matches of a pattern within the string. It returns the patterns in the order they are found. If there are no matches, then an empty list is returned.

Consider the following example.

Example

  1. import re  
  2.   
  3. str = "How are you. How is everything"  
  4.   
  5. matches = re.findall("How", str)  
  6.   
  7. print(matches)  
  8.   
  9. print(matches)  

Output:

['How', 'How']

The match object

The match object contains the information about the search and the output. If there is no match found, the None object is returned.

Example

  1. import re  
  2.   
  3. str = "How are you. How is everything"  
  4.   
  5. matches = re.search("How", str)  
  6.   
  7. print(type(matches))  
  8.   
  9. print(matches) #matches is the search object  

Output:

<class '_sre.SRE_Match'>
<_sre.SRE_Match object; span=(0, 3), match='How'>

The Match object methods

There are the following methods associated with the Match object.

  1. span(): It returns the tuple containing the starting and end position of the match.
  2. string(): It returns a string passed into the function.
  3. group(): The part of the string is returned where the match is found.

Example

  1. import re  
  2.   
  3. str = "How are you. How is everything"  
  4.   
  5. matches = re.search("How", str)  
  6.   
  7. print(matches.span())  
  8.   
  9. print(matches.group())  
  10.   
  11. print(matches.string)  

Output:

(0, 3)
How
How are you. How is everything





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Sending Email using SMTP

Simple Mail Transfer Protocol (SMTP) is used as a protocol to handle the email transfer using Python. It is used to route emails between email servers. It is an application layer protocol which allows to users to send mail to another. The receiver retrieves email using the protocols POP(Post Office Protocol) and IMAP(Internet Message Access Protocol).

Python Sending Email using SMTP

When the server listens for the TCP connection from a client, it initiates a connection on port 587.

Python provides a smtplib module, which defines an the SMTP client session object used to send emails to an internet machine. For this purpose, we have to import the smtplib module using the import statement.

  1. import smtplib  

The SMTP object is used for the email transfer. The following syntax is used to create the smtplib object.

  1. import smtplib     
  2. smtpObj = smtplib.SMTP(host, port, local_hostname)      

It accepts the following parameters.

  • host: It is the hostname of the machine which is running your SMTP server. Here, we can specify the IP address of the server like (https://www.javatpoint.com) or localhost. It is an optional parameter.
  • port: It is the port number on which the host machine is listening to the SMTP connections. It is 25 by default.
  • local_hostname: If the SMTP server is running on your local machine, we can mention the hostname of the local machine.

The sendmail() method of the SMTP object is used to send the mail to the desired machine. The syntax is given below.

  1. smtpObj.sendmail(sender, receiver, message)    

Example

  1. #!/usr/bin/python3    
  2. import smtplib    
  3. sender_mail = 'sender@fromdomain.com'    
  4. receivers_mail = ['reciever@todomain.com']    
  5. message = """From: From Person %s  
  6. To: To Person %s  
  7. Subject: Sending SMTP e-mail   
  8. This is a test e-mail message.  
  9. """%(sender_mail,receivers_mail)    
  10. try:    
  11.    smtpObj = smtplib.SMTP('localhost')    
  12.    smtpObj.sendmail(sender_mail, receivers_mail, message)    
  13.    print("Successfully sent email")    
  14. except Exception:    
  15.    print("Error: unable to send email")    

Sending email from gmail

There are cases where the emails are sent using the Gmail SMTP server. In this case, we can pass Gmail as the SMTP server instead of using the localhost with the port 587.

Use the following syntax.

  1. $ smtpObj = smtplib.SMTP("gmail.com"587)     

Here, we need to login to the Gmail account using Gmail user name and password. For this purpose, the smtplib provide the login() method, which accepts the username and password of the sender.

This may make your Gmail ask you for access to less secure apps if you're using Gmail. You will need to turn this ON temporarily for this to work.

Python Sending Email using SMTP

Consider the following example.

Example

  1. #!/usr/bin/python3    
  2. import smtplib    
  3. sender_mail = 'sender@gmail.com'    
  4. receivers_mail = ['reciever@gmail.com']    
  5. message = """From: From Person %s  
  6. To: To Person %s  
  7. Subject: Sending SMTP e-mail   
  8. This is a test e-mail message.  
  9. """%(sender_mail,receivers_mail)    
  10. try:    
  11.    password = input('Enter the password');    
  12.    smtpObj = smtplib.SMTP('gmail.com',587)    
  13.    smtpobj.login(sender_mail,password)    
  14.    smtpObj.sendmail(sender_mail, receivers_mail, message)    
  15.    print("Successfully sent email")    
  16. except Exception:    
  17.    print("Error: unable to send email")    

Sending HTML in email

We can format the HTML in the message by specifying the MIME version, content-type, and character set to send the HTML.

Consider the following example.

Example

  1. #!/usr/bin/python3    
  2. import smtplib    
  3. sender_mail = 'sender@fromdomain.com'    
  4. receivers_mail = ['reciever@todomain.com']    
  5. message = """From: From Person %s  
  6. To: To Person %s  
  7.   
  8. MIME-Version:1.0  
  9. Content-type:text/html  
  10.   
  11.   
  12. Subject: Sending SMTP e-mail   
  13.   
  14. <h3>Python SMTP</h3>  
  15. <strong>This is a test e-mail message.</strong>  
  16. """%(sender_mail,receivers_mail)    
  17. try:    
  18.    smtpObj = smtplib.SMTP('localhost')    
  19.    smtpObj.sendmail(sender_mail, receivers_mail, message)    
  20.    print("Successfully sent email")    
  21. except Exception:    
  22.    print("Error: unable to send email")    






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python read csv file

CSV File

A csv stands for "comma separated values", which is defined as a simple file format that uses specific structuring to arrange tabular data. It stores tabular data such as spreadsheet or database in plain text and has a common format for data interchange. A csv file opens into the excel sheet, and the rows and columns data define the standard format.

Python CSV Module Functions

The CSV module work is used to handle the CSV files to read/write and get data from specified columns. There are different types of CSV functions, which are as follows:

  • csv.field_size_limit - It returns the current maximum field size allowed by the parser.
  • csv.get_dialect - It returns the dialect associated with a name.
  • csv.list_dialects - It returns the names of all registered dialects.
  • csv.reader - It read the data from a csv file
  • csv.register_dialect - It associates dialect with a name. The name must be a string or a Unicode object.
  • csv.writer - It writes the data to a csv file
  • o csv.unregister_dialect - It deletes the dialect which is associated with the name from the dialect registry. If a name is not a registered dialect name, then an error is being raised.
  • csv.QUOTE_ALL - It instructs the writer objects to quote all fields. csv.QUOTE_MINIMAL - It instructs the writer objects to quote only those fields which contain special characters such as quotechar, delimiter, etc.
  • csv.QUOTE_NONNUMERIC - It instructs the writer objects to quote all the non-numeric fields.
  • csv.QUOTE_NONE - It instructs the writer object never to quote the fields.

Reading CSV files

Python provides various functions to read csv file. We are describing few method of reading function.

  • Using csv.reader() function

In Python, the csv.reader() module is used to read the csv file. It takes each row of the file and makes a list of all the columns.

We have taken a txt file named as python.txt that have default delimiter comma(,) with the following data:

  1. name,department,birthday month    
  2. Parker,Accounting,November    
  3. Smith,IT,October    

Example

  1. import csv    
  2. with open('python.csv') as csv_file:    
  3.     csv_reader = csv.reader(csv_file, delimiter=',')    
  4.     line_count = 0    
  5.     for row in csv_reader:    
  6.         if line_count == 0:    
  7.             print(f'Column names are {", ".join(row)}')    
  8.             line_count += 1    

Output:

Column names are name, department, birthday month
Parker works in the Accounting department, and was born in November.
Smith works in the IT department, and was born in October.
Processed 3 lines.

In the above code, we have opened 'python.csv' using the open() function. We used csv.reader() function to read the file, that returns an iterable reader object. The reader object have consisted the data and we iterated using for loop to print the content of each row

Read a CSV into a Dictionar

We can also use DictReader() function to read the csv file directly into a dictionary rather than deal with a list of individual string elements.

Again, our input file, python.txt is as follows:

  1. name,department,birthday month    
  2. Parker,Accounting,November    
  3. Smith,IT,October    

Example

  1. import csv      
  2. with open('python.txt', mode='r') as csv_file:    
  3.     csv_reader = csv.DictReader(csv_file)    
  4.     line_count = 0    
  5.     for row in csv_reader:    
  6.         if line_count == 0:    
  7.             print(f'The Column names are as follows {", ".join(row)}')    
  8.             line_count += 1    
  9.         print(f'\t{row["name"]} works in the {row["department"]} department, and was born in {row["birthday month"]}.')    
  10.         line_count += 1    
  11.     print(f'Processed {line_count} lines.')    

Output:

The Column names are as follows name, department, birthday month
Parker works in the Accounting department, and was born in November.
Smith works in the IT department, and was born in October.
Processed 3 lines.

Reading csv files with Pandas

The Pandas is defined as an open-source library which is built on the top of the NumPy library. It provides fast analysis, data cleaning, and preparation of the data for the user.

Reading the csv file into a pandas DataFrame is quick and straight forward. We don't need to write enough lines of code to open, analyze, and read the csv file in pandas and it stores the data in DataFrame.

Here, we are taking a slightly more complicated file to read, called hrdata.csv, which contains data of company employees.

  1. Name,Hire Date,Salary,Leaves Remaining    
  2. John Idle,08/15/14,50000.00,10    
  3. Smith Gilliam,04/07/15,65000.00,8    
  4. Parker Chapman,02/21/14,45000.00,10    
  5. Jones Palin,10/14/13,70000.00,3    
  6. Terry Gilliam,07/22/14,48000.00,7    
  7. Michael Palin,06/28/13,66000.00,8    

Example

  1. import pandas    
  2. df = pandas.read_csv('hrdata.csv')    
  3. print(df)    

In the above code, the three lines are enough to read the file, and only one of them is doing the actual work, i.e., pandas.read_csv()

Output:

 Name Hire Date Salary Leaves Remaining
0 John Idle 03/15/14 50000.0 10
1 Smith Gilliam 06/01/15 65000.0 8
2 Parker Chapman 05/12/14 45000.0 10
3 Jones Palin 11/01/13 70000.0 3
4 Terry Gilliam 08/12/14 48000.0 7
5 Michael Palin 05/23/13 66000.0 8






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Write CSV File

CSV File

A CSV stands for "comma-separated values", which is defined as a simple file format that uses specific structuring to arrange tabular data. It stores tabular data such as spreadsheet or database in plain text and has a standard format for data interchange. The CSV file opens into the excel sheet, and the rows and columns data define the standard format.

Python CSV Module Functions

The CSV module work is to handle the CSV files to read/write and get data from specified columns. There are different types of CSV functions, which are as follows:

  • csv.field_size_limit - It returns the current maximum field size allowed by the parser.
  • csv.get_dialect - Returns the dialect associated with a name.
  • csv.list_dialects - Returns the names of all registered dialects.
  • csv.reader - Read the data from a CSV file
  • csv.register_dialect - It associates dialect with a name, and name must be a string or a Unicode object.
  • csv.writer - Write the data to a CSV file
  • csv.unregister_dialect - It deletes the dialect, which is associated with the name from the dialect registry. If a name is not a registered dialect name, then an error is being raised.
  • csv.QUOTE_ALL - It instructs the writer objects to quote all fields.
  • csv.QUOTE_MINIMAL - It instructs the writer objects to quote only those fields which contain special characters such as quotechar, delimiter, etc.
  • csv.QUOTE_NONNUMERIC - It instructs the writer objects to quote all the non-numeric fields.
  • csv.QUOTE_NONE - It instructs the writer object never to quote the fields.

Writing CSV Files

We can also write any new and existing CSV files in Python by using the csv.writer() module. It is similar to the csv.reader() module and also has two methods, i.e., writer function or the Dict Writer class.

It presents two functions, i.e., writerow() and writerows(). The writerow() function only write one row, and the writerows() function write more than one row.
Dialects

It is defined as a construct that allows you to create, store, and re-use various formatting parameters. It supports several attributes; the most frequently used are:

  • Dialect.delimiter: This attribute is used as the separating character between the fields. The default value is a comma (,).
  • Dialect.quotechar: This attribute is used to quote fields that contain special characters.
  • Dialect.lineterminator: It is used to create new lines, and the default value is '\r\n'.

Let's write the following data to a CSV File.

  1. data = [{'Rank''B''first_name''Parker''last_name''Brian'},     
  2. {'Rank''A''first_name''Smith''last_name''Rodriguez'},    
  3. {'Rank''C''first_name''Tom''last_name''smith'},    
  4. {'Rank''B''first_name''Jane''last_name''Oscar'},     
  5. {'Rank''A''first_name''Alex''last_name''Tim'}]    

Example -

  1. import csv    
  2.      
  3. with open('Python.csv''w') as csvfile:    
  4.     fieldnames = ['first_name''last_name''Rank']    
  5.     writer = csv.DictWriter(csvfile, fieldnames=fieldnames)    
  6.      
  7.     writer.writeheader()    
  8.     writer.writerow({'Rank''B''first_name''Parker''last_name''Brian'})    
  9.     writer.writerow({'Rank''A''first_name''Smith',    
  10.                      'last_name''Rodriguez'})    
  11.     writer.writerow({'Rank''B''first_name''Jane''last_name''Oscar'})    
  12.     writer.writerow({'Rank''B''first_name''Jane''last_name''Loive'})    
  13.      
  14. print("Writing complete")    

Output:

Writing complete

It returns the file named as 'Python.csv' that contains the following data:

  1. first_name,last_name,Rank    
  2. Parker,Brian,B    
  3. Smith,Rodriguez,A    
  4. Jane,Oscar,B    
  5. Jane,Loive,B     

Write a CSV into a Dictionary

We can also use the class DictWriter to write the CSV file directly into a dictionary.

A file named as python.csv contains the following data:

Parker, Accounting, November

Smith, IT, October

Example -

  1. import csv    
  2. with open('python.csv', mode='w') as csv_file:    
  3.     fieldnames = ['emp_name''dept''birth_month']    
  4.     writer = csv.DictWriter(csv_file, fieldnames=fieldnames)    
  5.     writer.writeheader()    
  6.     writer.writerow({'emp_name''Parker''dept''Accounting''birth_month''November'})    
  7.     writer.writerow({'emp_name''Smith''dept''IT''birth_month''October'})    

Output:

emp_name,dept,birth_month
Parker,Accounting,November
Smith,IT,October

Writing CSV Files Using Pandas

Pandas is defined as an open source library which is built on the top of Numpy library. It provides fast analysis, data cleaning and preparation of the data for the user.

It is as easy as reading the CSV file using pandas. You need to create the DataFrame, which is a two-dimensional, heterogeneous tabular data structure and consists of three main components- data, columns, and rows. Here, we take a slightly more complicated file to read, called hrdata.csv, which contains data of company employees.

  1. Name,Hire Date,Salary,Leaves Remaining    
  2. John Idle,08/15/14,50000.00,10    
  3. Smith Gilliam,04/07/15,65000.00,8    
  4. Parker Chapman,02/21/14,45000.00,10    
  5. Jones Palin,10/14/13,70000.00,3    
  6. Terry Gilliam,07/22/14,48000.00,7    
  7. Michael Palin,06/28/13,66000.00,8    

Example -

  1. import pandas    
  2. df = pandas.read_csv('hrdata.csv',     
  3.             index_col='Employee',     
  4.             parse_dates=['Hired'],    
  5.             header=0,     
  6.             names=['Employee''Hired''Salary''Sick Days'])    
  7. df.to_csv('hrdata_modified.csv')    

Output:

Employee, Hired, Salary, Sick Days
John Idle, 2014-03-15, 50000.0,10
Smith Gilliam, 2015-06-01, 65000.0,8
Parker Chapman, 2014-05-12, 45000.0,10
Jones Palin, 2013-11-01, 70000.0,3
Terry Gilliam, 2014-08-12 , 48000.0,7
Michael Palin, 2013-05-23, 66000.0,8






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python read excel file

Excel is a spreadsheet application which is developed by Microsoft. It is an easily accessible tool to organize, analyze, and store the data in tables. It is widely used in many different applications all over the world. From Analysts to CEOs, various professionals use Excel for both quick stats and serious data crunching.

Excel Documents

An Excel spreadsheet document is called a workbook which is saved in a file with .xlsx extension. The first row of the spreadsheet is mainly reserved for the header, while the first column identifies the sampling unit. Each workbook can contain multiple sheets that are also called a worksheets. A box at a particular column and row is called a cell, and each cell can include a number or text value. The grid of cells with data forms a sheet.

The active sheet is defined as a sheet in which the user is currently viewing or last viewed before closing Excel.

Reading from an Excel file

First, you need to write a command to install the xlrd module.

  1. pip install xlrd    

Creating a Workbook

A workbook contains all the data in the excel file. You can create a new workbook from scratch, or you can easily create a workbook from the excel file that already exists.

Input File

We have taken the snapshot of the workbook.

Python Read Excel File

Code

  1. # Import the xlrd module      
  2. import xlrd     
  3.       
  4. # Define the location of the file     
  5. loc = ("path of file")     
  6.       
  7. # To open the Workbook     
  8. wb = xlrd.open_workbook(loc)     
  9. sheet = wb.sheet_by_index(0)     
  10.       
  11. # For row 0 and column 0     
  12. sheet.cell_value(00)  

Explanation: In the above example, firstly, we have imported the xlrd module and defined the location of the file. Then we have opened the workbook from the excel file that already exists.

Reading from the Pandas

Pandas is defined as an open-source library which is built on the top of the NumPy library. It provides fast analysis, data cleaning, and preparation of the data for the user and supports both xls and xlsx extensions from the URL.

It is a python package which provides a beneficial data structure called a data frame.

Example

  1. Example -   
  2. import pandas as pd    
  3.     
  4. # Read the file    
  5. data = pd.read_csv(".csv", low_memory=False)    
  6.     
  7. # Output the number of rows    
  8. print("Total rows: {0}".format(len(data)))    
  9.     
  10. # See which headers are available    
  11. print(list(data))    

Reading from the openpyxl

First, we need to install an openpyxl module using pip from the command line.

  1. pip install openpyxl    

After that, we need to import the module.

We can also read data from the existing spreadsheet using openpyxl. It also allows the user to perform calculations and add content that was not part of the original dataset.

Example

  1. import openpyxl    
  2. my_wb = openpyxl.Workbook()    
  3. my_sheet = my_wb.active    
  4. my_sheet_title = my_sheet.title    
  5. print("My sheet title: " + my_sheet_title)    

Output:

My sheet title: Sheet

To learn more about openpyxl, visit our complete tutorial Click Here. We have discussed essential detail in this tutorial.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Write Excel File

The Python write excel file is used to perform the multiple operations on a spreadsheet using the xlwt module. It is an ideal way to write data and format information to files with .xls extension.

If you want to write data to any file and don't want to go through the trouble of doing everything by yourself, then you can use a for loop to automate the whole process a little bit.

Write Excel File Using xlsxwriter Module

We can also write the excel file using the xlsxwriter module. It is defined as a Python module for writing the files in the XLSX file format. It can also be used to write text, numbers, and formulas to multiple worksheets. Also, it supports features such as charts, formatting, images, page setup, auto filters, conditional formatting, and many others.

We need to use the following command to install xlsxwriter module:

  1. pip install xlsxwriter     

Note- Throughout XlsxWriter, rows, and columns are zero-indexed. The first cell in a worksheet is listed as, A1 is (0,0), B1 is (0,1), A2 is (1,0), B2 is (1,1)......,and so on.

Write Excel File Using openpyxl Module

It is defined as a package which is generally recommended if you want to read and write .xlsx, xlsm, xltx, and xltm files. You can check it by running type(wb).

The load_workbook() function takes an argument and returns a workbook object, which represents the file. Make sure that you are in the same directory where your spreadsheet is located. Otherwise, you will get an error while importing.

You can easily use a for loop with the help of the range() function to help you to print out the values of the rows that have values in column 2. If those particular cells are empty, you will get None.

Writing data to Excel files with xlwt

You can use the xlwt package, apart from the XlsxWriter package to create the spreadsheets that contain your data. It is an alternative package for writing data, formatting information, etc. and ideal for writing the data and format information to files with .xls extension. It can perform multiple operations on the spreadsheet.

It supports features such as formatting, images, charts, page setup, auto filters, conditional formatting, and many others.

Pandas have excellent methods for reading all kinds of data from excel files. We can also import the results back to pandas.

Writing Files with pyexcel

You can easily export your arrays back to a spreadsheet by using the save_as() function and pass the array and name of the destination file to the dest_file_name argument.

It allows us to specify the delimiter and add dest_delimiter argument. You can pass the symbol that you want to use as a delimiter in-between " ".

Code

  1. import xlsxwriter module     
  2. import xlsxwriter     
  3.       
  4. book = xlsxwriter.Book('Example2.xlsx')     
  5. sheet = book.add_sheet()     
  6.        
  7. # Rows and columns are zero indexed.     
  8. row = 0    
  9. column = 0    
  10.       
  11. content = ["Parker""Smith""John"]     
  12.       
  13. # iterating through the content list     
  14. for item in content :     
  15.       
  16.     # write operation perform     
  17.     sheet.write(row, column, item)     
  18.       
  19.     # incrementing the value of row by one with each iterations.     
  20.     row += 1    
  21.           
  22. book.close()     

Output:

Python Write Excel File





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Assert Keyword

Python assert keyword is defined as a debugging tool that tests a condition. The Assertions are mainly the assumption that asserts or state a fact confidently in the program. For example, while writing a division function, the divisor should not be zero, and you assert that the divisor is not equal to zero.

It is merely a Boolean expression that has a condition or expression checks if the condition returns true or false. If it is true, the program does not do anything, and it moves to the next line of code. But if it is false, it raises an AssertionError exception with an optional error message.

The main task of assertions is to inform the developers about unrecoverable errors in the program like "file not found", and it is right to say that assertions are internal self-checks for the program. It is the most essential for the testing or quality assurance in any application development area. The syntax of the assert keyword is given below.

Syntax

  1. assert condition, error_message(optional)    

Why Assertion is used

It is a debugging tool, and its primary task is to check the condition. If it finds that the condition is true, it moves to the next line of code, and If not, then stops all its operations and throws an error. It points out the error in the code.

Where Assertion in Python used

  • Checking the outputs of the functions.
  • Used for testing the code.
  • In checking the values of arguments.Checking the valid input.

Example1

This example shows the working of assert with the error message.

  1. def avg(scores):    
  2.     assert len(scores) != 0,"The List is empty."    
  3.     return sum(scores)/len(scores)    
  4.     
  5. scores2 = [67,59,86,75,92]    
  6. print("The Average of scores2:",avg(scores2))    
  7.     
  8. scores1 = []    
  9. print("The Average of scores1:",avg(scores1))    

Output:

The Average of scores2: 75.8
AssertionError: The List is empty.

Explanation: In the above example, we have passed a non-empty list scores2 and an empty list scores1 to the avg() function. We received an output for scores2 list successfully, but after that, we got an error AssertionError: List is empty. The assert condition is satisfied by the scores2 list and lets the program continue to run. However, scores1 doesn't satisfy the condition and gives an AssertionError.

Example2:

This example shows the "Divide by 0 error" in the console.

  1. # initializing number     
  2. x = 7    
  3. y = 0    
  4. # It uses assert to check for 0     
  5. print ("x / y value is : ")     
  6. assert y != 0"Divide by 0 error"    
  7. print (x / y)     

Output:

x / y value is :

Runtime Exception :

Traceback (most recent call last):
File "main.py", line 6, in <module>
assert y != 0, "Divide by 0 error"
AssertionError: Divide by 0 error

Explanation:

In the above example, we have initialized an integer variable, i.e., x=7, y=0, and try to print the value of x/y as an output. The Python interpreter generated a Runtime Exception because of the assert keyword found the divisor as zero then displayed "Divide by 0 error" in the console.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python List Comprehension

List Comprehension is defined as an elegant way to define, create a list in Python and consists of brackets that contains an expression followed by for clause. It is efficient in both computationally and in terms of coding space and time.

Signature

The list comprehension starts with '[' and ']'.

[ expression for item in list if conditional ]

Example

  1. letters = []  
  2. for letter in 'Python':  
  3.     letters.append(letter)  
  4. print(letters)  

Output:

['P', 'y', 't', 'h', 'o', 'n']

Example

  1. letters = [ letter for letter in 'Python' ]  
  2. print( letters)  

Output:

['P', 'y', 't', 'h', 'o', 'n']

Example

  1. x = {'chrome''browser''Windows''OS''C''language'}  
  2. x['mouse'] = 'hardware'  
  3. print(x['Windows'])  

Output:

OS
Next TopicPython Tutorial





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Collection Module

The Python collection module is defined as a container that is used to store collections of data, for example - list, dict, set, and tuple, etc. It was introduced to improve the functionalities of the built-in collection containers.

Python collection module was first introduced in its 2.4 release.

There are different types of collection modules which are as follows:

namedtuple()

The Python namedtuple() function returns a tuple-like object with names for each position in the tuple. It was used to eliminate the problem of remembering the index of each field of a tuple object in ordinary tuples.

Examples

  1. pranshu = ('James'24'M')    
  2. print(pranshu)    

Output:

('James', 24, 'M')

OrderedDict()

The Python OrderedDict() is similar to a dictionary object where keys maintain the order of insertion. If we try to insert key again, the previous value will be overwritten for that key.

Example

  1. import collections    
  2. d1=collections.OrderedDict()    
  3. d1['A']=10    
  4. d1['C']=12    
  5. d1['B']=11    
  6. d1['D']=13    
  7.     
  8. for k,v in d1.items():    
  9.     print (k,v)    

Output:

A 10
C 12
B 11
D 13

defaultdict()

The Python defaultdict() is defined as a dictionary-like object. It is a subclass of the built-in dict class. It provides all methods provided by dictionary but takes the first argument as a default data type.

Example

  1. from collections import defaultdict      
  2. number = defaultdict(int)      
  3. number['one'] = 1      
  4. number['two'] = 2      
  5. print(number['three'])    

Output:

0

Counter()

The Python Counter is a subclass of dictionary object which helps to count hashable objects.

Example

  1. from collections import Counter      
  2. c = Counter()    
  3. list = [1,2,3,4,5,7,8,5,9,6,10]      
  4. Counter(list)    
  5. Counter({1:5,2:4})      
  6. list = [1,2,4,7,5,1,6,7,6,9,1]      
  7. c = Counter(list)      
  8. print(c[1])     

Output:

3

deque()

The Python deque() is a double-ended queue which allows us to add and remove elements from both the ends.

Example

  1. from collections import deque    
  2. list = ["x","y","z"]    
  3. deq = deque(list)    
  4. print(deq)    

Output:

deque(['x', 'y', 'z'])

Chainmap Objects

A chainmap class is used to groups multiple dictionary together to create a single list. The linked dictionary stores in the list and it is public and can be accessed by the map attribute. Consider the following example.

Example

  1. from collections import ChainMap  
  2. baseline = {'Name''Peter''Age''14'}  
  3. adjustments = {'Age''14''Roll_no''0012'}  
  4. print(list(ChainMap(adjustments, baseline)))  

Output:

['Name', 'Age', 'Roll_no' ]

UserDict Objects

The UserDict behaves as a wrapper around the dictionary objects. The dictionary can be accessed as an attribute by using the UserDict object. It provides the easiness to work with the dictionary.

It provides the following attribute.

data - A real dictionary used to store the contents of the UserDict class.

UserList Objects

The UserList behaves as a wrapper class around the list-objects. It is useful when we want to add new functionality to the lists. It provides the easiness to work with the dictionary.

It provides the following attribute.

data - A real list is used to store the contents of the User class.

UserString Objects

The UserList behaves as a wrapper class around the list objects. The dictionary can be accessed as an attribute by using the UserString object. It provides the easiness to work with the dictionary.

It provides the following attribute.

data - A real str object is used to store the contents of the UserString class.


Next TopicPython math module





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Math Module

Python math module is defined as the most famous mathematical functions, which includes trigonometric functions, representation functions, logarithmic functions, etc. Furthermore, it also defines two mathematical constants, i.e., Pie and Euler number, etc.

Pie (n): It is a well-known mathematical constant and defined as the ratio of circumstance to the diameter of a circle. Its value is 3.141592653589793.

Euler's number(e): It is defined as the base of the natural logarithmic, and its value is 2.718281828459045.

There are different math modules which are given below:

math.log()

This method returns the natural logarithm of a given number. It is calculated to the base e.

Example

  1. import math    
  2. number = 2e-7  # small value of of x    
  3. print('log(fabs(x), base) is :', math.log(math.fabs(number), 10))    

Output:

log(fabs(x), base) is : -6.698970004336019
<

math.log10()

This method returns base 10 logarithm of the given number and called the standard logarithm.

Example

  1. import math    
  2. x=13  # small value of of x    
  3. print('log10(x) is :', math.log10(x))    

Output:

log10(x) is : 1.1139433523068367

math.exp()

This method returns a floating-point number after raising e to the given number.

Example

  1. import math    
  2. number = 5e-2  # small value of of x    
  3. print('The given number (x) is :', number)    
  4. print('e^x (using exp() function) is :', math.exp(number)-1)    

Output:

The given number (x) is : 0.05
e^x (using exp() function) is : 0.05127109637602412

math.pow(x,y)

This method returns the power of the x corresponding to the value of y. If value of x is negative or y is not integer value than it raises a ValueError.

Example

  1. import math  
  2. number = math.pow(10,2)  
  3. print("The power of number:",number)  

Output:

The power of number: 100.0

math.floor(x)

This method returns the floor value of the x. It returns the less than or equal value to x.

Example:

  1. import math  
  2. number = math.floor(10.25201)  
  3. print("The floor value is:",number)  

Output:

The floor value is: 10

math.ceil(x)

This method returns the ceil value of the x. It returns the greater than or equal value to x.

  1. import math  
  2. number = math.ceil(10.25201)  
  3. print("The floor value is:",number)  

Output:

The floor value is: 11

math.fabs(x)

This method returns the absolute value of x.

  1. import math  
  2. number = math.fabs(10.001)  
  3. print("The floor absolute is:",number)  

Output:

The absolute value is: 10.001

math.factorial()

This method returns the factorial of the given number x. If x is not integral, it raises a ValueError.

Example

  1. import math  
  2. number = math.factorial(7)  
  3. print("The factorial of number:",number)  

Output:

The factorial of number: 5040

math.modf(x)

This method returns the fractional and integer parts of x. It carries the sign of x is float.

Example

  1. import math  
  2. number = math.modf(44.5)  
  3. print("The modf of number:",number)  

Output:

The modf of number: (0.5, 44.0)

Python provides the several math modules which can perform the complex task in single-line of code. In this tutorial, we have discussed a few important math modules.


Next TopicPython Tutorial





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python OS Module

Python OS module provides the facility to establish the interaction between the user and the operating system. It offers many useful OS functions that are used to perform OS-based tasks and get related information about operating system.

The OS comes under Python's standard utility modules. This module offers a portable way of using operating system dependent functionality.

The Python OS module lets us work with the files and directories.

  1. To work with the OS module, we need to import the OS module.  
  2. import os  

There are some functions in the OS module which are given below:

os.name()

This function provides the name of the operating system module that it imports.

Currently, it registers 'posix', 'nt', 'os2', 'ce', 'java' and 'riscos'.

Example

  1. import os   
  2. print(os.name)   

Output:

nt

os.mkdir()

The os.mkdir() function is used to create new directory. Consider the following example.

  1. import os  
  2. os.mkdir("d:\\newdir")  

It will create the new directory to the path in the string argument of the function in the D drive named folder newdir.

os.getcwd()

It returns the current working directory(CWD) of the file.

Example

  1. import os     
  2. print(os.getcwd())     

Output:

C:\Users\Python\Desktop\ModuleOS

os.chdir()

The os module provides the chdir() function to change the current working directory.

  1. import os  
  2. os.chdir("d:\\")  

Output:

d:\\

os.rmdir()

The rmdir() function removes the specified directory with an absolute or related path. First, we have to change the current working directory and remove the folder.

Example

  1. import os  
  2. # It will throw a Permission error; that's why we have to change the current working directory.  
  3. os.rmdir("d:\\newdir")  
  4. os.chdir("..")  
  5. os.rmdir("newdir")  

os.error()

The os.error() function defines the OS level errors. It raises OSError in case of invalid or inaccessible file names and path etc.

Example

  1. import os  
  2.   
  3. try:  
  4.     # If file does not exist,  
  5.     # then it throw an IOError  
  6.     filename = 'Python.txt'  
  7.     f = open(filename, 'rU')  
  8.     text = f.read()  
  9.     f.close()  
  10.   
  11. # The Control jumps directly to here if  
  12. # any lines throws IOError.  
  13. except IOError:  
  14.   
  15.     # print(os.error) will <class 'OSError'>  
  16.     print('Problem reading: ' + filename)     

Output:

Problem reading: Python.txt

os.popen()

This function opens a file or from the command specified, and it returns a file object which is connected to a pipe.

Example

  1. import os     
  2. fd = "python.txt"      
  3.     
  4. # popen() is similar to open()     
  5. file = open(fd, 'w')     
  6. file.write("This is awesome")     
  7. file.close()     
  8. file = open(fd, 'r')     
  9. text = file.read()     
  10. print(text)     
  11.       
  12. # popen() provides gateway and accesses the file directly     
  13. file = os.popen(fd, 'w')     
  14. file.write("This is awesome")     
  15. # File not closed, shown in next function.      

Output:

This is awesome

os.close()

This function closes the associated file with descriptor fr.

Example

  1. import os     
  2. fr = "Python1.txt"    
  3. file = open(fr, 'r')     
  4. text = file.read()     
  5. print(text)     
  6. os.close(file)       

Output:

Traceback (most recent call last):
File "main.py", line 3, in
file = open(fr, 'r')
FileNotFoundError: [Errno 2] No such file or directory: 'Python1.txt'

os.rename()

A file or directory can be renamed by using the function os.rename(). A user can rename the file if it has privilege to change the file.

Example

  1. import os     
  2. fd = "python.txt"    
  3. os.rename(fd,'Python1.txt')     
  4. os.rename(fd,'Python1.txt')     

Output:

Traceback (most recent call last):
File "main.py", line 3, in
os.rename(fd,'Python1.txt')
FileNotFoundError: [Errno 2] No such file or directory: 'python.txt' -> 'Python1.txt'

os.access()

This function uses real uid/gid to test if the invoking user has access to the path.

Example

  1. import os     
  2. import sys    
  3.     
  4. path1 = os.access("Python.txt", os.F_OK)     
  5. print("Exist path:", path1)     
  6.       
  7. # Checking access with os.R_OK     
  8. path2 = os.access("Python.txt", os.R_OK)     
  9. print("It access to read the file:", path2)     
  10.       
  11. # Checking access with os.W_OK     
  12. path3 = os.access("Python.txt", os.W_OK)     
  13. print("It access to write the file:", path3)     
  14.       
  15. # Checking access with os.X_OK     
  16. path4 = os.access("Python.txt", os.X_OK)     
  17. print("Check if path can be executed:", path4)    

Output:

Exist path: False
It access to read the file: False
It access to write the file: False
Check if path can be executed: False






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Random module

The Python random module functions depend on a pseudo-random number generator function random(), which generates the float number between 0.0 and 1.0.

There are different types of functions used in a random module which is given below:

random.random()

This function generates a random float number between 0.0 and 1.0.

random.randint()

This function returns a random integer between the specified integers.

random.choice()

This function returns a randomly selected element from a non-empty sequence.

Example

  1. # importing "random"  module.  
  2. import random  
  3. # We are using the choice() function to generate a random number from  
  4. # the given list of numbers.  
  5. print ("The random number from list is : ",end="")  
  6. print (random.choice([5041844031]))  

Output:

The random number from list is : 84

random.shuffle()

This function randomly reorders the elements in the list.

random.randrange(beg,end,step)

This function is used to generate a number within the range specified in its argument. It accepts three arguments, beginning number, last number, and step, which is used to skip a number in the range. Consider the following example.

  1. # We are using randrange() function to generate in range from 100  
  2. # to 500. The last parameter 10 is step size to skip  
  3. # ten numbers when selecting.  
  4. import random  
  5. print ("A random number from range is : ",end="")  
  6. print (random.randrange(10050010))  

Output:

A random number from range is : 290

random.seed()

This function is used to apply on the particular random number with the seed argument. It returns the mapper value. Consider the following example.

  1. # importing "random" module.  
  2. import random  
  3. # using random() to generate a random number  
  4. # between 0 and 1  
  5. print("The random number between 0 and 1 is : ", end="")  
  6. print(random.random())  
  7.   
  8. # using seed() to seed a random number  
  9. random.seed(4)  

Output:

The random number between 0 and 1 is : 0.4405576668981033






Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python statistics module

Python statistics module provides the functions to mathematical statistics of numeric data. There are some popular statistical functions defined in this module.

mean() function

The mean() function is used to calculate the arithmetic mean of the numbers in the list.

Example

  1. import statistics    
  2. # list of positive integer numbers   
  3. datasets = [5274268]     
  4. x = statistics.mean(datasets)     
  5. # Printing the mean   
  6. print("Mean is :", x)  

Output:

Mean is : 4.857142857142857

median() function

The median() function is used to return the middle value of the numeric data in the list.

Example

  1. import statistics     
  2. datasets = [4, -566945, -2]      
  3. # Printing median of the   
  4. # random data-set   
  5. print("Median of data-set is : % s "  
  6.         % (statistics.median(datasets)))  

Output:

Median of data-set is : 4.5

mode() function

The mode() function returns the most common data that occurs in the list.

Example

  1. import statistics     
  2. # declaring a simple data-set consisting of real valued positive integers.   
  3. dataset =[2477223668]     
  4. # Printing out the mode of given data-set   
  5. print("Calculated Mode % s" % (statistics.mode(dataset)))  

Output:

Calculated Mode 2

stdev() function

The stdev() function is used to calculate the standard deviation on a given sample which is available in the form of the list.

Example

  1. import statistics     
  2. # creating a simple data - set   
  3. sample = [7891011]     
  4. # Prints standard deviation   
  5. print("Standard Deviation of sample is % s "   
  6.                 % (statistics.stdev(sample)))   

Output:

Standard Deviation of sample is 1.5811388300841898

median_low()

The median_low function is used to return the low median of numeric data in the list.

Example

  1. import statistics     
  2. # simple list of a set of integers   
  3. set1 = [462577]     
  4. # Note: low median will always be a member of the data-set.     
  5. # Print low median of the data-set   
  6. print("Low median of data-set is % s "   
  7.         % (statistics.median_low(set1)))  

Output:

Low median of the data-set is 5

median_high()

The median_high function is used to return the high median of numeric data in the list.

Example

  1. import statistics     
  2. # list of set of the integers   
  3. dataset = [217619]     
  4. print("High median of data-set is %s "   
  5.         % (statistics.median_high(dataset)))  

Output:

High median of the data-set is 6
Next TopicPython Tutorial





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python sys module

The python sys module provides functions and variables which are used to manipulate different parts of the Python Runtime Environment. It lets us access system-specific parameters and functions.

import sys

First, we have to import the sys module in our program before running any functions.

sys.modules

This function provides the name of the existing python modules which have been imported.

sys.argv

This function returns a list of command line arguments passed to a Python script. The name of the script is always the item at index 0, and the rest of the arguments are stored at subsequent indices.

sys.base_exec_prefix

This function provides an efficient way to the same value as exec_prefix. If not running a virtual environment, the value will remain the same.

sys.base_prefix

It is set up during Python startup, before site.py is run, to the same value as prefix.

sys.byteorder

It is an indication of the native byteorder that provides an efficient way to do something.

sys.maxsize

This function returns the largest integer of a variable.

sys.path

This function shows the PYTHONPATH set in the current system. It is an environment variable that is a search path for all the python modules.

sys.stdin

It is an object that contains the original values of stdin at the start of the program and used during finalization. It can restore the files.

sys.getrefcount

This function returns the reference count of an object.

sys.exit

This function is used to exit from either the Python console or command prompt, and also used to exit from the program in case of an exception.

sys executable

The value of this function is the absolute path to a Python interpreter. It is useful for knowing where python is installed on someone else machine.

sys.platform

This value of this function is used to identify the platform on which we are working.

Next TopicPython Tutorial





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python IDEs

IDE stands for Integrated Development Environment is defined as a coding tool that helps to automate the process of editing, compiling, testing, etc. in an SDLC and it provides ease to the developer to run, write and debug the code.

It is specially designed for software development that consists of several tools which is used for developing and testing the software.

There are some Python IDEs which are as follows:

Python IDEs

PyCharm

Python IDEs

PyCharm was developed by the Jet Brains, and it is a cross-platform Integrated Development Environment (IDE) specially designed for python. It is the most widely used IDE and available in both paid version and free open-source as well. It saves ample time by taking care of routine tasks.

It is a complete python IDE that is loaded with a rich set of features like auto code completion, quick project navigation, fast error checking and correction, remote development support, database accessibility, etc.

Features

  • Smart code navigation
  • Errors Highlighting
  • Powerful debugger
  • Supports Python web development frameworks, i.e., Angular JS, Javascript

Spyder

Python IDEs

Spyder is an open-source that has high recognition in the IDE market and most suitable for data science. The full name of Spyder is Scientific Python Development Environment. It supports all the significant platforms Linux, Windows, and MacOS X.

It provides a set of features like localized code editor, document viewer, variable explorer, integrated console, etc. and supports no. of scientific modules like NumPy, SciPy, etc.

Features

  • Proper syntax highlighting and auto code completion
  • Integrates strongly with IPython console
  • Performs well in multi-language editor and auto code completion mode

PyDev

Python IDEs

PyDev is defined as one of the commonly used Python IDE, which is an external plugin for Eclipse. It is a natural choice of the Python developers that are coming from the Java background and very popular in the market as Python interpreter.

Aleksandar Totic is famous for his contribution to Mosaic browser and worked on Pydev project during 2003-2004.

Pydev has a feature which includes Django integration, automatic code completion, smart indents and block indents, etc.

Features

  • Strong Parameters like refactoring, debugging, code analysis, and code coverage function.
  • It supports virtual environments, Mypy, and black formatter.
  • Also supports PyLint integration, remote debugger, Unit test integration, etc.

Atom

Python IDEs

Atom is developed by GitHub, which is initially started as an open-source, cross-platform. It is based on a framework, i.e., Electron which enables cross-platform desktop application using Chromium and Node.js and generally known as "Hackable Text Editor for the 21st century".

Features

  • Visualize the results on Atom without open any other window.
  • A plugin named "Markdown Preview Plus" provides built-in support for editing and visualizing Markdown files.

Wing

Python IDEs

It is defined as a cross-platform IDE that is packed with necessary features and with decent development support. Its personal edition is free of cost. The pro version comes with a 30 days trial for the developers to try it out.

It has several features that include auto-completion, syntax highlighting, indents, and debugging.

Features

  • Customizable and can have extensions as well.
  • Supports remote development, test-driven development along with the unit test.

Jupyter Notebook

Python IDEs

Jupyter is one of the most used IPython notebook editors that is used across the Data Science industry. It is a web application that is based on the server-client structure and allows you to create and manipulate notebook documents. It makes the best use of the fact that python is an interpreted language.

Features

  • Supports markdowns
  • Easy creation and editing of codes
  • Ideal for beginners in data science

Thonny

Python IDEs

Thonny is another IDE which is best suited for learning and teaching programming. It is a software developed at the University of Tartu and supports code completion and highlight syntax errors.

Features

  • Simple debugger
  • Supports highlighting errors and auto code completion

Rodeo

Python IDEs

Rodeo is defined as one of the best IDE for python that is most widely used for data science projects like taking data and information from different resources.

It supports cross-platform functionality and provides auto-completion of code.

Features

  • Allows the functions for comparing data, interact, plot, and inspect data.
  • Auto code completion, syntax highlighter, visual file navigator, etc.

Microsoft Visual Studio

Python IDEs

Microsoft Visual Studio is an open-source code editor which was best suited for development and debugging of latest web and cloud projects. It has its own marketplace for extensions.

Features

  • Supports Python Coding in Visual studio
  • Available in both paid and free version

Eric Python

Python IDEs

The Eric Python is an editor which is developed in Python itself and can be used for both professional and non-professional work.

Features

  • Offers configurable window layout, editors, source code folding
  • Advanced project management capability, version control
  • In-built debugger and task management support
Next TopicPython Tutorial





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Arrays

An array is defined as a collection of items that are stored at contiguous memory locations. It is a container which can hold a fixed number of items, and these items should be of the same type. An array is popular in most programming languages like C/C++, JavaScript, etc.

Array is an idea of storing multiple items of the same type together and it makes easier to calculate the position of each element by simply adding an offset to the base value. A combination of the arrays could save a lot of time by reducing the overall size of the code. It is used to store multiple values in single variable. If you have a list of items that are stored in their corresponding variables like this:

car1 = "Lamborghini"

car2 = "Bugatti"

car3 = "Koenigsegg"

If you want to loop through cars and find a specific one, you can use the array.

The array can be handled in Python by a module named array. It is useful when we have to manipulate only specific data values. Following are the terms to understand the concept of an array:

Element - Each item stored in an array is called an element.

Index - The location of an element in an array has a numerical index, which is used to identify the position of the element.

Array Representation

An array can be declared in various ways and different languages. The important points that should be considered are as follows:

  • Index starts with 0.
  • We can access each element via its index.
  • The length of the array defines the capacity to store the elements.

Array operations

Some of the basic operations supported by an array are as follows:

  • Traverse - It prints all the elements one by one.
  • Insertion - It adds an element at the given index.
  • Deletion - It deletes an element at the given index.
  • Search - It searches an element using the given index or by the value.
  • Update - It updates an element at the given index.

The Array can be created in Python by importing the array module to the python program.

  1. from array import *  
  2. arrayName = array(typecode, [initializers])   

Accessing array elements

We can access the array elements using the respective indices of those elements.

  1. import array as arr  
  2. a = arr.array('i', [2468])  
  3. print("First element:", a[0])  
  4. print("Second element:", a[1])  
  5. print("Second last element:", a[-1])  

Output:

First element: 2
Second element: 4
Second last element: 8

Explanation: In the above example, we have imported an array, defined a variable named as "a" that holds the elements of an array and print the elements by accessing elements through indices of an array.

How to change or add elements

Arrays are mutable, and their elements can be changed in a similar way like lists.

  1. import array as arr  
  2. numbers = arr.array('i', [1235710])  
  3.    
  4. # changing first element  
  5. numbers[0] = 0     
  6. print(numbers)    # Output: array('i', [0235710])  
  7.    
  8. # changing 3rd to 5th element  
  9. numbers[2:5] = arr.array('i', [468])    
  10. print(numbers)    # Output: array('i', [0246810])  

Output:

array('i', [0, 2, 3, 5, 7, 10])
array('i' ,[0, 2, 4, 6, 8, 10])

Explanation: In the above example, we have imported an array and defined a variable named as "numbers" which holds the value of an array. If we want to change or add the elements in an array, we can do it by defining the particular index of an array where you want to change or add the elements.

Why to use arrays in Python?

A combination of arrays saves a lot of time. The array can reduce the overall size of the code.

How to delete elements from an array?

The elements can be deleted from an array using Python's del statement. If we want to delete any value from the array, we can do that by using the indices of a particular element.

  1. import array as arr  
  2. number = arr.array('i', [12334])  
  3. del number[2]                           # removing third element  
  4. print(number)                           # Output: array('i', [1234])  

Output:

array('i', [10, 20, 40, 60])

Explanation: In the above example, we have imported an array and defined a variable named as "number" which stores the values of an array. Here, by using del statement, we are removing the third element [3] of the given array.

Finding the length of an array

The length of an array is defined as the number of elements present in an array. It returns an integer value that is equal to the total number of the elements present in that array.

Syntax

  1. len(array_name)  

Array Concatenation

We can easily concatenate any two arrays using the + symbol.

Example

  1. a=arr.array('d',[1.1 , 2.1 ,3.1,2.6,7.8])  
  2. b=arr.array('d',[3.7,8.6])  
  3. c=arr.array('d')  
  4. c=a+b  
  5. print("Array c = ",c)  

Output:

Array c= array('d', [1.1, 2.1, 3.1, 2.6, 7.8, 3.7, 8.6])

Explanation

In the above example, we have defined variables named as "a, b, c" that hold the values of an array.

Example

  1. import array as arr  
  2. x = arr.array('i', [471922])  
  3. print("First element:", x[0])  
  4. print("Second element:", x[1])  
  5. print("Second last element:", x[-1])  

Output:

First element: 4
Second element: 7
Second last element: 22

Explanation: In the above example, first, we have imported an array and defined a variable named as "x" which holds the value of an array and then, we have printed the elements using the indices of an array.


Next TopicPython Tutorial





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Command line arguments

The Python supports the programs that can be run on the command line, complete with command line arguments. It is the input parameter that needs to be passed to the script when executing them.

It means to interact with a command-line interface for the scripts.

It provides a getopt module, in which command line arguments and options can be parsed.

What is argument passing?

The command ls is often used to get a summary of files and folders present in a particular directory.

Why to use argparse?

It means to communicate between the writer of a program and user which does not require going into the code and making changes to the script. It provides the ability to a user to enter into the command-line arguments.

Access command line arguments

The Python sys module provides access to command-line arguments via sys.argv. It solves the two purposes:

Python sys module

It is a basic module that was shipped with Python distribution from the early days on. It is a similar approach as C library using argc/argv to access the arguments. The sys module implements command-line arguments in a simple list structure named sys.argv.

Each list element represents a single argument. The first one -- sys.argv[0] -- is the name of Python script. The other list elements are sys.argv[1] to sys.argv[n]- are the command line arguments 2 to n. As a delimiter between arguments, space is used. Argument values that contain space in it have to be quoted, accordingly.

It stores command-line arguments into a list; we can access it using sys.argv. This is very useful and a simple way to read command-line arguments as String.

  1. import sys  
  2. print(type(sys.argv))  
  3. print('The command line arguments are:')  
  4. for i in sys.argv:  
  5. print(i)  

Python getopt module

The Python getopt module extends the separation of the input string by parameter validation. Based on getopt C function, it allows both short and long options, including a value assignment.

It is very similar to C getopt() function for parsing command line parameters.

It is useful in parsing command line arguments where we want the user to enter some options.

Code

  1. import getopt  
  2. import sys  
  3. argv = sys.argv[1:]  
  4. try:  
  5.     opts, args = getopt.getopt(argv, 'hm:d', ['help''my_file='])  
  6.     print(opts)  
  7.     print(args)  
  8. except getopt.GetoptError:  
  9.     # Print a message or do something useful  
  10.     print('Something went wrong!')  
  11.     sys.exit(2)  

Python argparse module

It offers a command-line interface with standardized output, whereas the former two solutions leave most of the work in your hands. argparse allows verification of fixed and optional arguments with a name checking as either UNIX or GNU style. It is the preferred way to parse command-line arguments. It provides a lot of option such as positional arguments, the default value for arguments, helps message, specifying the data type of argument etc.

It makes it easy to write the user-friendly command-line interfaces. It automatically generates help and usage messages and issues errors when a user gives invalid arguments to the program.

getopt.getopt method

This method is used for parsing the command line options and parameter list.

Syntax:

  1. getopt.getopt(args, options, [long_options])  

args- It is an argument list that needs to be parsed.

options- A string of option letters that the script wants to recognize, with options that require an argument which should be followed by a colon(:).

long_options(optional)- It must be a string with names of the long options, which should be supported.

  • This method returns a value consisting of two elements, i.e. list of (option, value) pairs, list of program arguments left after option list was stripped.
  • Each option-and-value pair are returned as an option as its first element, prefixed with a hyphen for short options (e.g.,'-x') or two hyphens for long options (e.g., '--long-option').

Exception getopt.GetoptError

This exception arises when an unrecognized option is found in the argument list or when any option requiring an argument is given none.

The argument to the exception is a string that indicates the cause of the error. The attributes msg and opt to give the error message and related option.

Code

  1. #!/usr/bin/python  
  2. import sys, getopt  
  3. def main(argv):  
  4.    inputfile = ''  
  5.    outputfile = ''  
  6.    try:  
  7.       opts, args = getopt.getopt(argv,"hi:o:",["ifile=","ofile="])  
  8.    except getopt.GetoptError:  
  9.       print 'test.py -i <inputfile> -o <outputfile>'  
  10.       sys.exit(2)  
  11.    for opt, arg in opts:  
  12.       if opt == '-h':  
  13.          print 'test.py -i <inputfile> -o <outputfile>'  
  14.          sys.exit()  
  15.       elif opt in ("-i""--ifile"):  
  16.          inputfile = arg  
  17.       elif opt in ("-o""--ofile"):  
  18.          outputfile = arg  
  19.    print 'Input file is "', inputfile  
  20.    print 'Output file is "', outputfile  
  21.   
  22. if __name__ == "__main__":  
  23.    main(sys.argv[1:])  

Output:

$ test.py -h
usage: test.py -i <inputfile> -o <outputfile>

$ test.py -i BMP -o
usage: test.py -i <inputfile> -o <outputfile>

$ test.py -i inputfile
Input file is " inputfile
Output file is "

How to use command line arguments in python?

Module Use Python version
sys All arguments in sys.argv (basic) All
argparse Build a command line interface >= 2.3
docopt Created command line interfaces >= 2.5
fire Automatically generate command line interfaces (CLIs) All
optparse Deprecated < 2.7

Docopt

Docopt is used to create command line interfaces.

  1. from docopt import docopt  
  2. if __name__ == '__main__':  
  3. arguments = docopt(__doc__, version='Example 1')  
  4. print(arguments)   

Fire

Python Fire automatically generates a command line interface; you only need one line of code. Unlike the other modules, it works instantly.

You don't need to define any arguments; all the methods are linked by default.

To install it type:

  1. pip install fire  

Define or use a class:

  1. import fire  
  2. class Python(object):  
  3.     def hello(self):  
  4.     print("Hello")  
  5.        def openfile(self, filename):  
  6.         print("Open file '" + filename + "'")  
  7.   
  8. if __name__ == '__main__':  
  9.     fire.Fire(Python)   

You have the options matching to the class methods:

  1. python example.py hello  
  2. python example.py openfile filename.txt  

Next TopicPython Tutorial





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python magic method

Python magic method is defined as the special method which adds "magic" to a class. It starts and ends with double underscores, for example, _init_ or _str_.

The built-in classes define many magic methods. The dir() function can be used to see the number of magic methods inherited by a class. It has two prefixes, and suffix underscores in the method name.

It is most frequently used to define the overloaded behaviors of predefined operators.

__init__

The _init_ method is called after the instance of the class has been created but before it returned to the caller. It is invoked without any call, when an instance of the class is created like constructors in other programming languages such as C++, Java, C#, PHP, etc. These methods are also known as initialize and are called after _new_. Its where you should initialize the instance variables.

__str__

This function computes "informal" or a nicely printable string representation of an object and must return a string object.

__repr__

This function is called by the repr() built-in function to compute the "official" string representation of an object and returns a machine-readable representation of a type. The goal of the _repr_ is to be unambiguous.

__len__

This function should return the length of an object.

__call__

We can make an object callable by adding the _call_ magic method, and it is another method that is not needed quite as often is _call_.

If defined in a class, then that class can be called. But if it was a function, instance itself rather than modifying.

__del__

Just as _init_, which is a constructor method, _del_ is like a destructor. If you have opened a file in _init _, then _del_ can close it.

__bytes__

It offers to compute a byte-string representation of an object and should return a string object.

__ge__

This method gets invoked when >= operator is used and returns True or False.

__neg__

This function gets called for the unary operator.

__ipow__

This function gets called on the exponents with arguments. e.g. a**=b.

__le__

This function gets called on comparison using <= operator.

_nonzero_

This function returns the Boolean value of the object. It gets invoked when the bool (self) function is called.


Next TopicPython Tutorial





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Stack and Queue

Data structure organizes the storage in computers so that we can easily access and change data. Stacks and Queues are the earliest data structure defined in computer science. A simple Python list can act as a queue and stack as well. A queue follows FIFO rule (First In First Out) and used in programming for sorting. It is common for stacks and queues to be implemented with an array or linked list.

Stack

A Stack is a data structure that follows the LIFO(Last In First Out) principle. To implement a stack, we need two simple operations:

  • push - It adds an element to the top of the stack.
  • pop - It removes an element from the top of the stack.
Python Stack and Queue Python Stack and Queue

Operations:

  • Adding - It adds the items in the stack and increases the stack size. The addition takes place at the top of the stack.
  • Deletion - It consists of two conditions, first, if no element is present in the stack, then underflow occurs in the stack, and second, if a stack contains some elements, then the topmost element gets removed. It reduces the stack size.
  • Traversing - It involves visiting each element of the stack.

Characteristics:

  • Insertion order of the stack is preserved.
  • Useful for parsing the operations.
  • Duplicacy is allowed.

Code

  1. # Code to demonstrate Implementation of   
  2. # stack using list   
  3. x = ["Python""C""Android"]   
  4. x.push("Java")   
  5. x.push("C++")   
  6. print(x)   
  7. print(x.pop())   
  8. print(x)   
  9. print(x.pop())   
  10. print(x)   

Output:

['Python', 'C', 'Android', 'Java', 'C++']
C++
['Python', 'C', 'Android', 'Java']
Java
['Python', 'C', 'Android']

Queue

A Queue follows the First-in-First-Out (FIFO) principle. It is opened from both the ends hence we can easily add elements to the back and can remove elements from the front.

To implement a queue, we need two simple operations:

  • enqueue - It adds an element to the end of the queue.
  • dequeue - It removes the element from the beginning of the queue.
Python Stack and Queue Python Stack and Queue

Operations on Queue

  • Addition - It adds the element in a queue and takes place at the rear end, i.e., at the back of the queue.
  • Deletion - It consists of two conditions - If no element is present in the queue, Underflow occurs in the queue, or if a stack contains some elements then element present at the front gets deleted.
  • Traversing - It involves to visit each element of the queue.

Characteristics

  • Insertion order of the queue is preserved.
  • Duplicacy is allowed.
  • Useful for parsing CPU task operations.

Note: The implementation of a queue is a little bit different. A queue follows the "First-In-First-Out". Time plays an important factor here. The Stack is fast because we insert and pop the elements from the end of the list, whereas in the queue, the insertion and pops are made from the beginning of the list, so it becomes slow. The cause of this time difference is due to the properties of the list, which is fast in the end operation but slow at the beginning operations because all other elements have to be shifted one by one.

Code

  1. import queue   
  2. # Queue is created as an object 'L'  
  3. L = queue.Queue(maxsize=10)   
  4.   
  5. # Data is inserted in 'L' at the end using put()   
  6. L.put(9)   
  7. L.put(6)   
  8. L.put(7)   
  9. L.put(4)   
  10. # get() takes data from   
  11. # from the head    
  12. # of the Queue   
  13. print(L.get())   
  14. print(L.get())   
  15. print(L.get())   
  16. print(L.get())   

Output:

9
6
7
4

Next TopicPython Tutorial





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

PySpark MLlib

Machine Learning is a technique of data analysis that combines data with statistical tools to predict the output. This prediction is used by the various corporate industries to make a favorable decision.

PySpark provides an API to work with the Machine learning called as mllib. PySpark's mllib supports various machine learning algorithms like classification, regression clustering, collaborative filtering, and dimensionality reduction as well as underlying optimization primitives. Various machine learning concepts are given below:

  • classification

The pyspark.mllib library supports several classification methods such as binary classification, multiclass classification, and regression analysis. The object may belong to a different class. The objective of classification is to differentiate the data based on the information. Random Forest, Naive Bayes, Decision Tree are the most useful algorithms in classification.

  • clustering

Clustering is an unsupervised machine learning problem. It is used when you do not know how to classify the data; we require the algorithm to find patterns and classify the data accordingly. The popular clustering algorithms are the K-means clustering, Gaussian mixture model, Hierarchical clustering.

  • fpm

The fpm means frequent pattern matching, which is used for mining various items, itemsets, subsequences, or other substructure. It is mostly used in large-scale datasets.

  • linalg

The mllib.linalg utilities are used for linear algebra.

  • recommendation

It is used to define the relevant data for making a recommendation. It is capable of predicting future preference and recommending the top items. For example, Online entertainment platform Netflix has a huge collection of movies, and sometimes people face difficulty in selecting the favorite items. This is the field where the recommendation plays an important role.

  • mllib regression

The regression is used to find the relationship and dependencies between variables. It finds the correlation between each feature of data and predicts the future values.

The mllib package supports many other algorithms, classes, and functions. Here we will understand the basic concept of pyspak.mllib.

MLlib Features

The PySpark mllib is useful for iterative algorithms. The features are the following:

  • Extraction: It extracts features from "row" data.
  • Transformation: It is used for scaling, converting, or modifying features.
  • Selection: Selecting a useful subset from a larger set of features.
  • Locality Sensitive Hashing: It combines aspects of feature transformation with other algorithms.

Let's have a look at the essential libraries of PySpark MLlib.

MLlib Linear Regression

Linear regression is used to find the relationship and dependencies between variables. Consider the following code:

  1. frompyspark.sql import SparkSession  
  2. spark = SparkSession.builder.appName('Customer').getOrCreate()  
  3. frompyspark.ml.regression import LinearRegression  
  4. dataset = spark.read.csv(r'C:\Users\DEVANSH SHARMA\Ecommerce-Customers.csv')  
  5. dataset.show(10)  

Output:

+--------------------+--------------------+----------------+------------------+------------------+------------------+--------------------+-------------------+
| _c0| _c1| _c2| _c3| _c4| _c5| _c6| _c7|
+--------------------+--------------------+----------------+------------------+------------------+------------------+--------------------+-------------------+
| Email| Address| Avatar|Avg Session Length| Time on App| Time on Website|Length of Membership|Yearly Amount Spent|
|mstephenson@ferna...|835 Frank TunnelW...| Violet| 34.49726772511229| 12.65565114916675| 39.57766801952616| 4.0826206329529615| 587.9510539684005|
| hduke@hotmail.com|4547 Archer Commo...| DarkGreen| 31.92627202636016|11.109460728682564|37.268958868297744| 2.66403418213262| 392.2049334443264|
| pallen@yahoo.com|24645 Valerie Uni...| Bisque|33.000914755642675|11.330278057777512|37.110597442120856| 4.104543202376424| 487.54750486747207|
|riverarebecca@gma...|1414 David Throug...| SaddleBrown| 34.30555662975554|13.717513665142507| 36.72128267790313| 3.120178782748092| 581.8523440352177|
|mstephens@davidso...|14023 Rodriguez P...|MediumAquaMarine| 33.33067252364639|12.795188551078114| 37.53665330059473| 4.446308318351434| 599.4060920457634|
|alvareznancy@luca...|645 Martha Park A...| FloralWhite|33.871037879341976|12.026925339755056| 34.47687762925054| 5.493507201364199| 637.102447915074|
|katherine20@yahoo...|68388 Reyes Light...| DarkSlateBlue| 32.02159550138701|11.366348309710526| 36.68377615286961| 4.685017246570912| 521.5721747578274|
| awatkins@yahoo.com|Unit 6538 Box 898...| Aqua|32.739142938380326| 12.35195897300293| 37.37335885854755| 4.4342734348999375| 549.9041461052942|
|vchurch@walter-ma...|860 Lee KeyWest D...| Salmon| 33.98777289568564|13.386235275676436|37.534497341555735| 3.2734335777477144| 570.2004089636196|
+--------------------+--------------------+----------------+------------------+------------------+------------------+--------------------+-------------------+
only showing top 10 rows

In the following code, we are importing the VectorAssembler library to create a new column Independent feature:

  1. frompyspark.ml.linalg import Vectors  
  2. frompyspark.ml.feature import VectorAssembler  
  3. featureassembler = VectorAssembler(inputCols = ["Avg Session Length","Time on App","Time on Website"],outputCol = "Independent Features")  
  4. output = featureassembler.transform(dataset)  
  5. output.show()  

Output:

+------------------+
Independent Feature
+------------------+
|34.49726772511229 |
|31.92627202636016 |
|33.000914755642675|
|34.30555662975554 |
|33.33067252364639 |
|33.871037879341976|
|32.02159550138701 |
|32.739142938380326|
|33.98777289568564 |
+------------------+

  1. z = featureassembler.transform(dataset)  
  2. finlized_data = z.select("Indepenent feature""Yearly Amount Spent",)  
  3. z.show()  

Output:

+--------------------++-------------------+
|Independent Feature | Yearly Amount Spent|
+--------------------++-------------------+
|34.49726772511229 | 587.9510539684005 |
|31.92627202636016 | 392.2049334443264 |
|33.000914755642675 | 487.5475048674720 |
|34.30555662975554 | 581.8523440352177 |
|33.33067252364639 | 599.4060920457634 |
|33.871037879341976 | 637.102447915074 |
|32.02159550138701 | 521.5721747578274 |
|32.739142938380326 | 549.9041461052942 |
|33.98777289568564 | 570.2004089636196 |
+--------------------++-------------------+

PySpark provides the LinearRegression() function to find the prediction of any given dataset. The syntax is given below:

  1. regressor = LinearRegression(featureCol = 'column_name1', labelCol = 'column_name2 ')  

MLlib K- Mean Cluster

The K- Mean cluster algorithm is one of the most popular and commonly used algorithms. It is used to cluster the data points into a predefined number of clusters. The below example is showing the use of MLlib K-Means Cluster library:

  1. from pyspark.ml.clustering import KMeans  
  2. from pyspark.ml.evaluation import ClusteringEvaluator  
  3. # Loads data.  
  4. dataset = spark.read.format("libsvm").load(r"C:\Users\DEVANSH SHARMA\Iris.csv")  
  5. # Trains a k-means model.  
  6. kmeans = KMeans().setK(2).setSeed(1)  
  7. model = kmeans.fit(dataset)  
  8. # Make predictions  
  9. predictions = model.transform(dataset)  
  10. # Evaluate clustering by computing Silhouette score  
  11. evaluator = ClusteringEvaluator()  
  12. silhouette = evaluator.evaluate(predictions)  
  13. print("Silhouette with squared euclidean distance = " + str(silhouette))  
  14. # Shows the result.  
  15. centers = model.clusterCenters()  
  16. print("Cluster Centers: ")  
  17. for center in centers:  
  18.     print(center)  

Parameters of PySpark MLlib

The few important parameters of PySpark MLlib are given below:

  • Ratings

It is RDD of Ratings or (userID, productID, rating) tuple.

  • Rank

It represents Rank of the computed feature matrices (number of features).

  • Iterations

It represents the number of iterations of ALS. (default: 5)

  • Lambda

It is the Regularization parameter. (default : 0.01)

  • Blocks

It is used to parallelize the computation of some number of blocks.

Collaborative Filtering (mllib.recommendation)

Collaborative filtering is a technique that is generally used for a recommender system. This technique is focused on filling the missing entries of a user-item. Association matrix spark.ml currently supports model-based collaborative filtering. In collaborative filtering, users and products are described by a small set of hidden factors that can be used to predict missing entries.

Scaling of the regularization parameter

The regularization parameter regParam is scaled to solve least-squares problem. The least-square problem occurs when the number of ratings are user-generated in updating user factors, or the number of ratings the product received in updating product factors.

Cold-start strategy

The ALS Model (Alternative Least Square Model) is used for prediction while making a common prediction problem. The problem encountered when user or items in the test dataset occurred that may not be present during training the model. It can occur in the two scenarios which are given below:

  • In the prediction, the model is not trained for users and items that have no rating history (it is called a cold-start strategy).
  • The data is splitted between training and evaluation sets during cross-validation. It is widespread to encounter users and items in the evaluation set that are not in the training set.
Let's consider the following example, where we load ratings data from the MovieLens dataset. Each row is containing a user, a movie, rating and a timestamp.

  1. #importing the libraries  
  2. frompyspark.ml.evaluation import RegressionEvaluator  
  3. frompyspark.ml.recommendation import ALS  
  4. frompyspark.sql import Row  
  5. no_of_lines = spark.read.text(r"C:\Users\DEVANSH SHARMA\MovieLens.csv").rdd  
  6. no_of_parts = no_of_lines.map(lambda row: row.value.split("::"))  
  7. ratingsRDD = no_of_lines.map(lambda p: Row(userId=int(p[0]), movieId=int(p[1]),  
  8.                                      rating=float(p[2]), timestamp=long(p[3])))  
  9. ratings = spark.createDataFrame(ratingsRDD)  
  10. (training, test) = ratings.randomSplit([0.80.2])  
  11.   
  12. # Develop the recommendation model using ALS on the training data  
  13. # Note we set cold start strategy to make sure that we don't get NaN evaluation metrics.  
  14. als = ALS(maxIter=5, regParam=0.01, userCol="userId", itemCol="movieId", ratingCol="rating",  
  15.     coldStartStrategy="drop")  
  16. model = als.fit(training)  
  17.   
  18. # Calculate the model by computing the RMSE on the test data  
  19. predictions = model.transform(test)  
  20. evaluator = RegressionEvaluator(metricName="rmse", labelCol="rating",  
  21. predictionCol="prediction")  
  22. rmse = evaluator.evaluate(predictions)  
  23. print("Root-mean-square error = " + str(rmse))  
  24.   
  25. # Evaluate top 10 movie recommendations for each user  
  26. userRecs = model.recommendForAllUsers(10)  
  27. # Evaluate top 10 user recommendations for each movie  
  28. movieRecs = model.recommendForAllItems(10)  
  29. # Evaluate top 10 movie recommendations for a specified set of users  
  30. users = ratings.select(als.getUserCol()).distinct().limit(3)  
  31. userSubsetRecs = model.recommendForUserSubset(users, 10)  
  32. # Evalute top 10 user recommendations for a specified set of movies  
  33. movies = ratings.select(als.getItemCol()).distinct().limit(3)  
  34. movieSubSetRecs = model.recommendForItemSubset(movies, 10)  

Next TopicPython Decorator





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Decorator

Decorators are one of the most helpful and powerful tools of Python. These are used to modify the behavior of the function. Decorators provide the flexibility to wrap another function to expand the working of wrapped function, without permanently modifying it.

In Decorators, functions are passed as an argument into another function and then called inside the wrapper function.

It is also called meta programming where a part of the program attempts to change another part of program at compile time.

Before understanding the Decorator, we need to know some important concepts of Python.

What are the functions in Python?

Python has the most interesting feature that everything is treated as an object even classes or any variable we define in Python is also assumed as an object. Functions are first-class objects in the Python because they can reference to, passed to a variable and returned from other functions as well. The example is given below:

  1. def func1(msg):  
  2.     print(msg)  
  3. func1("Hii")  
  4. func2 = func1  
  5. func2("Hii")  

Output:

Hii
Hii

In the above program, when we run the code it give the same output for both functions. The func2 referred to function func1 and act as function. We need to understand the following concept of the function:

  • The function can be referenced and passed to a variable and returned from other functions as well.
  • The functions can be declared inside another function and passed as an argument to another function.

Inner Function

Python provides the facility to define the function inside another function. These types of functions are called inner functions. Consider the following example:

  1. def func():  
  2.      print("We are in first function")  
  3.      def func1():  
  4.            print("This is first child function")  
  5.      def func2():  
  6.            print(" This is second child function")  
  7.      func1()  
  8.      func2()  
  9. func()  

Output:

We are in first function
This is first child function
This is second child function

In the above program, it doesn't matter how the child functions are declared. The execution of the child function makes effect on the output. These child functions are locally bounded with the func() so they cannot be called separately.

A function that accepts other function as an argument is also called higher order function. Consider the following example:

  1. def add(x):  
  2.     return x+1  
  3. def sub(x):  
  4.     return x-1  
  5. def operator(func, x):  
  6.     temp = func(x)  
  7.     return temp  
  8. print(operator(sub,10))  
  9. print(operator(add,20))  

Output:

9
21

In the above program, we have passed the sub() function and add() function as argument in operator() function.

A function can return another function. Consider the below example:

  1. def hello():  
  2.     def hi():  
  3.         print("Hello")  
  4.     return hi  
  5. new = hello()  
  6. new()  

Output:

Hello

In the above program, the hi() function is nested inside the hello() function. It will return each time we call hi().

Decorating functions with parameters

Let's have an example to understand the parameterized decorator function:

  1. def divide(x,y):  
  2.     print(x/y)  
  3. def outer_div(func):  
  4.     def inner(x,y):  
  5.         if(x<y):  
  6.             x,y = y,x  
  7.            return func(x,y)  
  8.      return inner  
  9. divide1 = outer_div(divide)  
  10. divide1(2,4)  

Output:

2.0

Syntactic Decorator

In the above program, we have decorated out_div() that is little bit bulky. Instead of using above method, Python allows to use decorator in easy way with @symbol. Sometimes it is called "pie" syntax.

  1. def outer_div(func):  
  2.     def inner(x,y):  
  3.         if(x<y):  
  4.            x,y = y,x  
  5.           return func(x,y)  
  6.      return inner  
  7. # syntax of generator  
  8. @outer_div  
  9. def divide(x,y):  
  10.      print(x/y)  

Output:

2.0

Reusing Decorator

We can reuse the decorator as well by recalling that decorator function. Let's make the decorator to its own module that can be used in many other functions. Creating a file called mod_decorator.py with the following code:

  1. def do_twice(func):  
  2.     def wrapper_do_twice():  
  3.         func()  
  4.         func()  
  5.     return wrapper_do_twice  

We can import mod_decorator.py in other file.

  1. from decorator import do_twice  
  2. @do_twice  
  3. def say_hello():  
  4.     print("Hello There")  
  5. say_hello()  

Output:

Hello There
Hello There

Python Decorator with Argument

We want to pass some arguments in function. Let's do it in following code:

  1. from decorator import do_twice  
  2. @do_twice  
  3. def display(name):  
  4.      print(f"Hello {name}")  
  5. display()  

Output:

TypeError: display() missing 1 required positional argument: 'name'

As we can see that, the function didn't accept the argument. Running this code raises an error. We can fix this error by using *args and **kwargs in the inner wrapper function. Modifying the decorator.py as follows:

  1. def do_twice(func):  
  2.     def wrapper_function(*args,**kwargs):  
  3.         func(*args,**kwargs)  
  4.         func(*args,**kwargs)  
  5.    return wrapper_function  

Now wrapper_function() can accept any number of argument and pass them on the function.

  1. from decorator import do_twice  
  2. @do_twice  
  3. def display(name):  
  4.       print(f"Hello {name}")  
  5. display("John")  

Output:

Hello John
Hello John

Returning Values from Decorated Functions

We can control the return type of the decorated function. The example is given below:

  1. from decorator import do_twice  
  2. @do_twice  
  3. def return_greeting(name):  
  4.      print("We are created greeting")  
  5.      return f"Hi {name}"  
  6. hi_adam = return_greeting("Adam")  

Output:

We are created greeting
We are created greeting

Fancy Decorators

Let's understand the fancy decorators by the following topic:

Class Decorators

Python provides two ways to decorate a class. Firstly, we can decorate the method inside a class; there are built-in decorators like @classmethod, @staticmethod and @property in Python. The @classmethod and @staticmethod define methods inside class that is not connected to any other instance of a class. The @property is generally used to modify the getters and setters of a class attributes. Let’s understand it by the following example:

Example: 1- @property decorator - By using it, we can use the class function as an attribute. Consider the following code:

  1. class Student:  
  2.     def __init__(self,name,grade):  
  3.          self.name = name  
  4.          self.grade = grade  
  5.     @property  
  6.     def display(self):  
  7.          return self.name + " got grade " + self.grade  
  8.   
  9. stu = Student("John","B")  
  10. print("Name:", stu.name)  
  11. print("Grade:", stu.grade)  
  12. print(stu.display)  

Output:

Name: John
Grade: B
John got grade B

Example:2 - @staticmethod decorator- The @staticmethod is used to define a static method in the class. It is called by using the class name as well as instance of the class. Consider the following code:

  1. class Person:  
  2.      @staticmethod  
  3.      def hello():  
  4.           print("Hello Peter")  
  5. per = Person()  
  6. per.hello()  
  7. Person.hello()  

Output:

Hello Peter
Hello Peter

Singleton Class

A singleton class only has one instance. There are many singletons in Python including True, None, etc.

Nesting Decorators

We can use multiple decorators by using them on top of each other. Let's consider the following example:

  1. @function1  
  2. @function2  
  3. def function(name):  
  4.       print(f "{name}")  

In the above code, we have used the nested decorator by stacking them onto one another.

Decorator with Arguments

It is always useful to pass arguments in a decorator. The decorator can be executed several times according to the given value of the argument. Let us consider the following example:

  1. Import functools  
  2.   
  3. def repeat(num):  
  4.   
  5. #Creating and returning a wrapper function  
  6.     def decorator_repeat(func):  
  7.         @functools.wraps(func)  
  8.         def wrapper(*args,**kwargs):  
  9.             for _ in range(num):  
  10.                 value = func(*args,**kwargs)  
  11.              return value  
  12.           return wrapper  
  13.     return decorator_repeat  
  14.   
  15. #Here we are passing num as an argument which repeats the print function  
  16. @repeat(num=5)  
  17. def function1(name):  
  18.      print(f"{name}")  

Output:

JavatPoint
JavatPoint
JavatPoint
JavatPoint
JavatPoint

In the above example, @repeat refers to a function object that can be called in another function. The @repeat(num = 5) will return a function which acts as a decorator.

The above code may look complex but it is the most commonly used decorator pattern where we have used one additional def that handles the arguments to the decorator.

Note: Decorator with argument is not frequently used in programming, but it provides flexibility. We can use it with or without argument.

Stateful Decorators

Stateful decorators are used to keep track of the decorator state. Let us consider the example where we are creating a decorator that counts how many times the function has been called.

  1. Import functools  
  2.   
  3. def count_function(func):  
  4. @functools.wraps(func)  
  5. def wrapper_count_calls(*args, **kwargs):  
  6. wrapper_count_calls.num_calls += 1  
  7.   
  8. print(f"Call{wrapper_count_calls.num_calls} of {func.__name__!r}")  
  9. return func(*args, **kwargs)  
  10.   
  11. wrapper_count_calls.num_calls = 0  
  12. return wrapper_count_calls  
  13.   
  14. @count_function  
  15. def say_hello():  
  16. print("Say Hello")  
  17.   
  18. say_hello()  
  19. say_hello()  

Output:

Call 1 of 'say_hello'
Say Hello
Call 2 of 'say_hello'
Say Hello

In the above program, the state represented the number of calls of the function stored in .num_calls on the wrapper function. When we call say_hello() it will display the number of the call of the function.

Classes as Decorators

The classes are the best way to maintain state. In this section, we will learn how to use a class as a decorator. Here we will create a class that contains __init__() and take func as an argument. The class needs to be callable so that it can stand in for the decorated function.

To making a class callable, we implement the special __call__() method.

  1. import functools  
  2.   
  3. class Count_Calls:  
  4. def __init__(self, func):  
  5. functools.update_wrapper(self, func)  
  6. self.func = func  
  7. self.num_calls = 0  
  8.   
  9. def __call__(self, *args, **kwargs):  
  10. self.num_calls += 1  
  11. print(f"Call{self.num_calls} of {self.func.__name__!r}")  
  12. return self.func(*args, **kwargs)  
  13.   
  14. @Count_Calls  
  15. def say_hello():  
  16. print("Say Hello")  
  17.   
  18. say_hello()  
  19. say_hello()  
  20. say_hello()  

Output:

Call 1 of 'say_hello'
Say Hello
Call 2 of 'say_hello'
Say Hello
Call 3 of 'say_hello'
Say Hello

The __init__() method stores a reference to the function and can do any other required initialization.


Next TopicPython Generators





Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Python Generators

What is Python Generator?

Python Generators are the functions that return the traversal object and used to create iterators. It traverses the entire items at once. The generator can also be an expression in which syntax is similar to the list comprehension in Python.

There is a lot of complexity in creating iteration in Python; we need to implement __iter__() and __next__() method to keep track of internal states.

It is a lengthy process to create iterators. That's why the generator plays an essential role in simplifying this process. If there is no value found in iteration, it raises StopIteration exception.

How to Create Generator function in Python?

It is quite simple to create a generator in Python. It is similar to the normal function defined by the def keyword and uses a yield keyword instead of return. Or we can say that if the body of any function contains a yield statement, it automatically becomes a generator function. Consider the following example:

  1. def simple():  
  2. for i in range(10):  
  3.     if(i%2==0):  
  4.          yield i  
  5.   
  6. #Successive Function call using for loop  
  7. for i in simple():  
  8.     print(i)  

Output:

0
2
4
6
8

yield vs. return

The yield statement is responsible for controlling the flow of the generator function. It pauses the function execution by saving all states and yielded to the caller. Later it resumes execution when a successive function is called. We can use the multiple yield statement in the generator function.

The return statement returns a value and terminates the whole function and only one return statement can be used in the function.

Using multiple yield Statement

We can use the multiple yield statement in the generator function. Consider the following example.

  1. def multiple_yield():  
  2.     str1 = "First String"  
  3.     yield str1  
  4.   
  5.     str2 = "Second string"  
  6.     yield str2  
  7.   
  8.     str3 = "Third String"  
  9.     yield str3  
  10. obj = multiple_yield()  
  11. print(next(obj))  
  12. print(next(obj))  
  13. print(next(obj))  

Output:

First String
Second string
Third String

Difference between Generator function and Normal function

  • Normal function contains only one Lreturn statement whereas generator function can contain one or more yield statement.
  • When the generator functions are called, the normal function is paused immediately and control transferred to the caller.
  • Local variable and their states are remembered between successive calls.
  • StopIteration exception is raised automatically when the function terminates.

Generator Expression

We can easily create a generator expression without using user-defined function. It is the same as the lambda function which creates an anonymous function; the generator's expressions create an anonymous generator function.

The representation of generator expression is similar to the Python list comprehension. The only difference is that square bracket is replaced by round parentheses. The list comprehension calculates the entire list, whereas the generator expression calculates one item at a time.

Consider the following example:

  1. list = [1,2,3,4,5,6,7]  
  2.   
  3. # List Comprehension  
  4. z = [x**3 for x in list]  
  5.   
  6. # Generator expression  
  7. a = (x**3 for x in list)  
  8.   
  9. print(a)  
  10. print(z)  

Output:

<generator object <genexpr> at 0x01BA3CD8>
[1, 8, 27, 64, 125, 216, 343]

In the above program, list comprehension has returned the list of cube of elements whereas generator expression has returned the reference of calculated value. Instead of applying a for loop, we can also call next() on the generator object. Let's consider another example:

  1. list = [1,2,3,4,5,6]  
  2.   
  3. z = (x**3 for x in list)  
  4.   
  5. print(next(z))  
  6.   
  7. print(next(z))  
  8.   
  9. print(next(z))  
  10.   
  11. print(next(z))  

Output:

1
8
27
64

Note:- When we call the next(), Python calls __next__() on the function in which we have passed it as a parameter.

In the above program, we have used the next() function, which returned the next item of the list.

Example: Write a program to print the table of the given number using the generator.

  1. def table(n):  
  2.     for i in range(1,11):  
  3.         yield n*i  
  4.            i = i+1  
  5.   
  6. for i in table(15):  
  7.     print(i)  

Output:

15
30
45
60
75
90
105
120
135
150

In the above example, a generator function is iterating using for loop.

Advantages of Generators

There are various advantages of Generators. Few of them are given below:

1. Easy to implement

Generators are easy to implement as compared to the iterator. In iterator, we have to implement __iter__() and __next__() function.

2. Memory efficient

Generators are memory efficient for a large number of sequences. The normal function returns a sequence of the list which creates an entire sequence in memory before returning the result, but the generator function calculates the value and pause their execution. It resumes for successive call. An infinite sequence generator is a great example of memory optimization. Let's discuss it in the below example by using sys.getsizeof() function.

  1. import sys  
  2. # List comprehension  
  3. nums_squared_list = [i * 2 for i in range(1000)]  
  4. print(sys.getsizeof("Memory in Bytes:"nums_squared_list))  
  5. # Generator Expression  
  6. nums_squared_gc = (i ** 2 for i in range(1000))  
  7. print(sys.getsizeof("Memory in Bytes:", nums_squared_gc))  

Output:

Memory in Bytes: 4508
Memory in Bytes: 56

We can observe from the above output that list comprehension is using 4508 bytes of memory, whereas generator expression is using 56 bytes of memory. It means that generator objects are much efficient than the list compression.

3. Pipelining with Generators

Data Pipeline provides the facility to process large datasets or stream of data without using extra computer memory.

Suppose we have a log file from a famous restaurant. The log file has a column (4th column) that keeps track of the number of burgers sold every hour and we want to sum it to find the total number of burgers sold in 4 years. In that scenario, the generator can generate a pipeline with a series of operations. Below is the code for it:

  1. with open('sells.log') as file:  
  2. burger_col = (line[3for line in file)  per_hour = (int(x) for x in burger_col if x != 'N/A')  
  3. print("Total burgers sold = ",sum(per_hour))  

4. Generate Infinite Sequence

The generator can produce infinite items. Infinite sequences cannot be contained within the memory and since generators produce only one item at a time, consider the following example:

  1. def infinite_sequence():  
  2.     num = 0  
  3.     while True:  
  4.         yield num  
  5.             num += 1  
  6.   
  7. for i in infinite_sequence():  
  8.     print(i)  

Output:

0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
.........
..........
315
316
317
Traceback (most recent call last):
File "C:\Users\DEVANSH SHARMA\Desktop\generator.py", line 33, in <module>
print(i)
KeyboardInterrupt

In this tutorial, we have learned about the Python Generators.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback

  • Send your Feedback to feedback@javatpoint.com

Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA






Javatpoint Logo
Sort by:
Relevance
Relevance
Date
Javatpoint Logo
Sort by:
Relevance
Relevance
Date

Web Scraping Using Python

What is Web Scraping?

Web Scraping is a technique to extract a large amount of data from several websites. The term "scraping" refers to obtaining the information from another source (webpages) and saving it into a local file. For example: Suppose you are working on a project called "Phone comparing website," where you require the price of mobile phones, ratings, and model names to make comparisons between the different mobile phones. If you collect these details by checking various sites, it will take much time. In that case, web scrapping plays an important role where by writing a few lines of code you can get the desired results.

Web Scraping Using Python

Web Scrapping extracts the data from websites in the unstructured format. It helps to collect these unstructured data and convert it in a structured form.

Startups prefer web scrapping because it is a cheap and effective way to get a large amount of data without any partnership with the data selling company.

Is Web Scrapping legal?

Here the question arises whether the web scrapping is legal or not. The answer is that some sites allow it when used legally. Web scraping is just a tool you can use it in the right way or wrong way.

Web scrapping is illegal if someone tries to scrap the nonpublic data. Nonpublic data is not reachable to everyone; if you try to extract such data then it is a violation of the legal term.

There are several tools available to scrap data from websites, such as:

  • Scrapping-bot
  • Scrapper API
  • Octoparse
  • Import.io
  • Webhose.io
  • Dexi.io
  • Outwit
  • Diffbot
  • Content Grabber
  • Mozenda
  • Web Scrapper Chrome Extension

Why Web Scrapping?

Web Scraping Using Python

As we have discussed above, web scrapping is used to extract the data from websites. But we should know how to use that raw data. That raw data can be used in various fields. Let's have a look at the usage of web scrapping:

  • Dynamic Price Monitoring

It is widely used to collect data from several online shopping sites and compare the prices of products and make profitable pricing decisions. Price monitoring using web scrapped data gives the ability to the companies to know the market condition and facilitate dynamic pricing. It ensures the companies they always outrank others.

  • Market Research

eb Scrapping is perfectly appropriate for market trend analysis. It is gaining insights into a particular market. The large organization requires a great deal of data, and web scrapping provides the data with a guaranteed level of reliability and accuracy.

  • Email Gathering

Many companies use personals e-mail data for email marketing. They can target the specific audience for their marketing.

  • News and Content Monitoring

A single news cycle can create an outstanding effect or a genuine threat to your business. If your company depends on the news analysis of an organization, it frequently appears in the news. So web scraping provides the ultimate solution to monitoring and parsing the most critical stories. News articles and social media platform can directly influence the stock market.

  • Social Media Scrapping

Web Scrapping plays an essential role in extracting data from social media websites such as Twitter, Facebook, and Instagram, to find the trending topics.

  • Research and Development

The large set of data such as general information, statistics, and temperature is scrapped from websites, which is analyzed and used to carry out surveys or research and development.

Why use Python for Web Scrapping?

There are other popular programming languages, but why we choose the Python over other programming languages for web scraping? Below we are describing a list of Python's features that make the most useful programming language for web scrapping.

  • Dynamically Typed

In Python, we don't need to define data types for variables; we can directly use the variable wherever it requires. It saves time and makes a task faster. Python defines its classes to identify the data type of variable.

  • Vast collection of libraries

Python comes with an extensive range of libraries such as NumPy, Matplotlib, Pandas, Scipy, etc., that provide flexibility to work with various purposes. It is suited for almost every emerging field and also for web scrapping for extracting data and do manipulation.

  • Less Code

The purpose of the web scrapping is to save time. But what if you spend more time in writing the code? That's why we use Python, as it can perform a task in a few lines of code.

  • Open-Source Community

Python is open-source, which means it is freely available for everyone. It has one of the biggest communities across the world where you can seek help if you get stuck anywhere in Python code.

The basics of web scraping

The web scrapping consists of two parts: a web crawler and a web scraper. In simple words, the web crawler is a horse, and the scrapper is the chariot. The crawler leads the scrapper and extracts the requested data. Let's understand about these two components of web scrapping:

  • The crawler

Web Scraping Using Python A web crawler is generally called a "spider." It is an artificial intelligence technology that browses the internet to index and searches for the content by given links. It searches for the relevant information asked by the programmer.

  • The scrapper
  • Web Scraping Using PythonA web scraper is a dedicated tool that is designed to extract the data from several websites quickly and effectively. Web scrappers vary widely in design and complexity, depending on the projects.

    How does Web Scrapping work?

    These are the following steps to perform web scraping. Let's understand the working of web scraping.

    Step -1: Find the URL that you want to scrape

    First, you should understand the requirement of data according to your project. A webpage or website contains a large amount of information. That's why scrap only relevant information. In simple words, the developer should be familiar with the data requirement.

    Step - 2: Inspecting the Page

    The data is extracted in raw HTML format, which must be carefully parsed and reduce the noise from the raw data. In some cases, data can be simple as name and address or as complex as high dimensional weather and stock market data.

    Step - 3: Write the code

    Write a code to extract the information, provide relevant information, and run the code.

    Step - 4: Store the data in the file

    Store that information in required csv, xml, JSON file format.

    Getting Started with Web Scrapping

    Python has a vast collection of libraries and also provides a very useful library for web scrapping. Let's understand the required library for Python.

    Library used for web scrapping

    • Selenium- Selenium is an open-source automated testing library. It is used to check browser activities. To install this library, type the following command in your terminal.
    1. pip install selenium  

    Note - It is good to use the PyCharm IDE.

    Web Scraping Using Python
    • Pandas

    Pandas library is used for data manipulation and analysis. It is used to extract the data and store it in the desired format.

    • BeautifulSoup
    BeautifulSoup is a Python library that is used to pull data of HTML and XML files. It is mainly designed for web scrapping. It works with the parser to provide a natural way of navigating, searching, and modifying the parse tree. The latest version of BeautifulSoup is 4.8.1.

    Let's understand the BeautifulSoup library in detail.

    Installation of BeautifulSoup

    You can install BeautifulSoup by typing the following command:

    1. pip install bs4  

    Installing a parser

    BeautifulSoup supports HTML parser and several third-party Python parsers. You can install any of them according to your dependency. The list of BeautifulSoup's parsers is the following:

    Parser Typical usage
    Python's html.parser BeautifulSoup(markup,"html.parser")
    lxml's HTML parser BeautifulSoup(markup,"lxml")
    lxml's XML parser BeautifulSoup(markup,"lxml-xml")
    Html5lib BeautifulSoup(markup,"html5lib")

    We recommend you to install html5lib parser because it is much suitable for the newer version of Python, or you can install lxml parser.

    Type the following command in your terminal:

    1. pip install html5lib  

    Web Scraping Using Python

    BeautifulSoup is used to transform a complex HTML document into a complex tree of Python objects. But there are a few essential types object which are mostly used:

    • Tag

    A Tag object corresponds to an XML or HTML original document.

    1. soup = bs4.BeautifulSoup("<b class = "boldest">Extremely bold</b>)  
    2. tag = soup.b  
    3. type(tag)  

    Output:

    <class "bs4.element.Tag">
    

    Tag contains lot of attributes and methods, but most important features of a tag are name and attribute.

    • Name

    Every tag has a name, accessible as .name:

    1. tag.name  
    • Attributes

    A tag may have any number of attributes. The tag <b id = "boldest"> has an attribute "id" whose value is "boldest". We can access a tag's attributes by treating the tag as dictionary.

    1. tag[id]  

    We can add, remove, and modify a tag's attributes. It can be done by using tag as dictionary.

    1. # add the element  
    2. tag['id'] = 'verybold'  
    3. tag['another-attribute'] = 1  
    4. tag  
    5. # delete the tag  
    6. del tag['id']     
    • Multi-valued Attributes

    In HTML5, there are some attributes that can have multiple values. The class (consists more than one css) is the most common multivalued attributes. Other attributes are rel, rev, accept-charset, headers, and accesskey.

    1. class_is_multi= { '*' : 'class'}  
    2. xml_soup = BeautifulSoup('<p class="body strikeout"></p>''xml', multi_valued_attributes=class_is_multi)  
    3. xml_soup.p['class']  
    4. # [u'body', u'strikeout']  
    • NavigableString

    A string in BeautifulSoup refers text within a tag. BeautifulSoup uses the NavigableString class to contain these bits of text.

    1. tag.string  
    2. # u'Extremely bold'  
    3. type(tag.string)  
    4. # <class 'bs4.element.NavigableString'>  

    A string is immutable means it can't be edited. But it can be replaced with another string using replace_with().

    1. tag.string.replace_with("No longer bold")  
    2. tag  

    In some cases, if you want to use a NavigableString outside the BeautifulSoup, the unicode() helps it to turn into normal Python Unicode string.

    • BeautifulSoup object

    The BeautifulSoup object represents the complete parsed document as a whole. In many cases, we can use it as a Tag object. It means it supports most of the methods described in navigating the tree and searching the tree.

    1. doc=BeautifulSoup("<document><content/>INSERT FOOTER HERE</document","xml")  
    2. footer=BeautifulSoup("<footer>Here's the footer</footer>","xml")  
    3. doc.find(text="INSERT FOOTER HERE").replace_with(footer)  
    4. print(doc)  

    Output:

    ?xml version="1.0" encoding="utf-8"?>
    # <document><content/><footer>Here's the footer</footer></document>
    

    Web Scrapping Example:

    Let's take an example to understand the scrapping practically by extracting the data from the webpage and inspecting the whole page.

    First, open your favorite page on Wikipedia and inspect the whole page, and before extracting data from the webpage, you should ensure your requirement. Consider the following code:

    1. #importing the BeautifulSoup Library  
    2.   
    3. importbs4  
    4. import requests  
    5.   
    6. #Creating the requests  
    7.   
    8. res = requests.get("https://en.wikipedia.org/wiki/Machine_learning")  
    9. print("The object type:",type(res))  
    10.   
    11. # Convert the request object to the Beautiful Soup Object  
    12. soup = bs4.BeautifulSoup(res.text,'html5lib')  
    13. print("The object type:",type(soup)  

    Output:

    The object type <class 'requests.models.Response'>
    Convert the object into: <class 'bs4.BeautifulSoup'>
    

    In the following lines of code, we are extracting all headings of a webpage by class name. Here front-end knowledge plays an essential role in inspecting the webpage.

    1. soup.select('.mw-headline')  
    2. for i in soup.select('.mw-headline'):  
    3. print(i.text,end = ',')  

    Output:

    Overview,Machine learning tasks,History and relationships to other fields,Relation to data mining,Relation to optimization,Relation to statistics, Theory,Approaches,Types of learning algorithms,Supervised learning,Unsupervised learning,Reinforcement learning,Self-learning,Feature learning,Sparse dictionary learning,Anomaly detection,Association rules,Models,Artificial neural networks,Decision trees,Support vector machines,Regression analysis,Bayesian networks,Genetic algorithms,Training models,Federated learning,Applications,Limitations,Bias,Model assessments,Ethics,Software,Free and open-source software,Proprietary software with free and open-source editions,Proprietary software,Journals,Conferences,See also,References,Further reading,External links,
    

    In the above code, we imported the bs4 and requested the library. In the third line, we created a res object to send a request to the webpage. As you can observe that we have extracted all heading from the webpage.

    Web Scraping Using Python

    Webpage of Wikipedia Learning

    Let's understand another example; we will make a GET request to the URL and create a parse Tree object (soup) with the use of BeautifulSoup and Python built-in "html5lib" parser.

    Here we will scrap the webpage of given link (https://www.javatpoint.com/). Consider the following code:

    1. following code:  
    2. # importing the libraries  
    3. from bs4 import BeautifulSoup  
    4. import requests  
    5.   
    6. url="https://www.javatpoint.com/"  
    7.   
    8. # Make a GET request to fetch the raw HTML content  
    9. html_content = requests.get(url).text  
    10.   
    11. # Parse the html content  
    12. soup = BeautifulSoup(html_content, "html5lib")  
    13. print(soup.prettify()) # print the parsed data of html  

    The above code will display the all html code of javatpoint homepage.

    Using the BeautifulSoup object, i.e. soup, we can collect the required data table. Let's print some interesting information using the soup object:

    • Let's print the title of the web page.
    1. print(soup.title)  

    Output: It will give an output as follow:

    <title>Tutorials List - Javatpoint</title>
    
    • In the above output, the HTML tag is included with the title. If you want text without tag, you can use the following code:
    1. print(soup.title.text)  

    Output: It will give an output as follow:

    Tutorials List - Javatpoint
    
    • We can get the entire link on the page along with its attributes, such as href, title, and its inner Text. Consider the following code:
    1. for link in soup.find_all("a"):  
    2. print("Inner Text is: {}".format(link.text))  
    3. print("Title is: {}".format(link.get("title")))  
    4. print("href is: {}".format(link.get("href")))  

    Output: It will print all links along with its attributes. Here we display a few of them:

    href is: https://www.facebook.com/javatpoint
    Inner Text is:
    The title is: None
    href is: https://twitter.com/pagejavatpoint
    Inner Text is:
    The title is: None
    href is: https://www.youtube.com/channel/UCUnYvQVCrJoFWZhKK3O2xLg
    Inner Text is:
    The title is: None
    href is: https://javatpoint.blogspot.com
    Inner Text is: Learn Java
    Title is: None
    href is: https://www.javatpoint.com/java-tutorial
    Inner Text is: Learn Data Structures
    Title is: None
    href is: https://www.javatpoint.com/data-structure-tutorial
    Inner Text is: Learn C Programming
    Title is: None
    href is: https://www.javatpoint.com/c-programming-language-tutorial
    Inner Text is: Learn C++ Tutorial
    

    Demo: Scraping Data from Flipkart Website

    In this example, we will scrap the mobile phone prices, ratings, and model name from Flipkart, which is one of the popular e-commerce websites. Following are the prerequisites to accomplish this task:

    Prerequisites:

    • Python 2.x or Python 3.x with Selenium, BeautifulSoup, Pandas libraries installed.
    • Google - chrome browser
    • Scrapping Parser such as html.parser, xlml, etc.

    Step - 1: Find the desired URL to scrap

    The initial step is to find the URL that you want to scrap. Here we are extracting mobile phone details from the flipkart. The URL of this page is https://www.flipkart.com/search?q=iphones&otracker=search&otracker1=search&marketplace=FLIPKART&as-show=on&as=off.

    Step -2: Inspecting the page

    It is necessary to inspect the page carefully because the data is usually contained within the tags. So we need to inspect to select the desired tag. To inspect the page, right-click on the element and click "inspect".

    Step - 3: Find the data for extracting

    Extract the Price, Name, and Rating, which are contained in the "div" tag, respectively.

    Step - 4: Write the Code

    1. from bs4 import BeautifulSoupas soup  
    2. from urllib.request import urlopen as uReq  
    3.   
    4. # Request from the webpage  
    5. myurl = "https://www.flipkart.com/search?q=iphones&otracker=search&otracker1=search&marketplace=FLIPKART&as-show=on&as=off"  
    6.   
    7.   
    8. uClient  = uReq(myurl)  
    9. page_html = uClient.read()  
    10. uClient.close()  
    11.   
    12. page_soup = soup(page_html, features="html.parser")  
    13.   
    14. # print(soup.prettify(containers[0]))  
    15.   
    16. # This variable held all html of webpage  
    17. containers = page_soup.find_all("div",{"class""_3O0U0u"})  
    18. # container = containers[0]  
    19. # # print(soup.prettify(container))  
    20. #  
    21. # price = container.find_all("div",{"class""col col-5-12 _2o7WAb"})  
    22. # print(price[0].text)  
    23. #  
    24. # ratings = container.find_all("div",{"class""niH0FQ"})  
    25. # print(ratings[0].text)  
    26. #  
    27. # #  
    28. # # print(len(containers))  
    29. # print(container.div.img["alt"])  
    30.   
    31. # Creating CSV File that will store all data   
    32. filename = "product1.csv"  
    33. f = open(filename,"w")  
    34.   
    35. headers = "Product_Name,Pricing,Ratings\n"  
    36. f.write(headers)  
    37.   
    38. for container in containers:  
    39.     product_name = container.div.img["alt"]  
    40.   
    41.     price_container = container.find_all("div", {"class""col col-5-12 _2o7WAb"})  
    42.     price = price_container[0].text.strip()  
    43.   
    44.     rating_container = container.find_all("div",{"class":"niH0FQ"})  
    45.     ratings = rating_container[0].text  
    46.   
    47. # print("product_name:"+product_name)  
    48.     # print("price:"+price)  
    49.     # print("ratings:"+ str(ratings))  
    50.   
    51.      edit_price = ''.join(price.split(','))  
    52.      sym_rupee = edit_price.split("?")  
    53.      add_rs_price = "Rs"+sym_rupee[1]  
    54.      split_price = add_rs_price.split("E")  
    55.      final_price = split_price[0]  
    56.   
    57.      split_rating = str(ratings).split(" ")  
    58.      final_rating = split_rating[0]  
    59.   
    60.      print(product_name.replace(",""|")+","+final_price+","+final_rating+"\n")  
    61. f.write(product_name.replace(",""|")+","+final_price+","+final_rating+"\n")  
    62.   
    63. f.close()  

    Output:

    Web Scraping Using Python

    We scrapped the details of the iPhone and saved those details in the CSV file as you can see in the output. In the above code, we put a comment on the few lines of code for testing purpose. You can remove those comments and observe the output.

    In this tutorial, we have discussed all basic concepts of web scrapping and described the sample scrapping from the leading online ecommerce site flipkart.


    Next TopicPython JSON





    Youtube For Videos Join Our Youtube Channel: Join Now

    Feedback

    • Send your Feedback to feedback@javatpoint.com

    Help Others, Please Share

    facebook twitter pinterest

    Learn Latest Tutorials


    Preparation


    Trending Technologies


    B.Tech / MCA






    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date
    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date

    Python JSON

    JSON stands for JavaScript Object Notation, which is a widely used data format for data interchange on the web. JSON is the ideal format for organizing data between a client and a server. Its syntax is similar to the JavaScript programming language. The main objective of JSON is to transmit the data between the client and the web server. It is easy to learn and the most effective way to interchange the data. It can be used with various programming languages such as Python, Perl, Java, etc.

    JSON mainly supports 6 types of data type In JavaScript:

    • String
    • Number
    • Boolean
    • Null
    • Object
    • Array

    JSON is built on the two structures:

    • It stores data in the name/value pairs. It is treated as an object, record, dictionary, hash table, keyed list.
    • The ordered list of values is treated as an array, vector, list, or sequence.

    JSON data representation is similar to the Python dictionary. Below is an example of JSON data:

    1. {  
    2.  "book": [  
    3.   {   
    4.        "id"01,  
    5. "language""English",  
    6. "edition""Second",  
    7. "author""Derrick Mwiti"   
    8. ],  
    9.    {  
    10.   {   
    11.     "id"02,  
    12. "language""French",  
    13. "edition""Third",  
    14. "author""Vladimir"   
    15. }  
    16. }  

    Working with Python JSON

    Python provides a module called json. Python supports standard library marshal and pickle module, and JSON API behaves similarly as these library. Python natively supports JSON features.

    The encoding of JSON data is called Serialization. Serialization is a technique where data transforms in the series of bytes and transmitted across the network.

    The deserialization is the reverse process of decoding the data that is converted into the JSON format.

    This module includes many built-in functions.

    Let's have a look at these functions:

    1. import json  
    2. print(dir(json))  

    Output:

    ['JSONDecodeError', 'JSONDecoder', 'JSONEncoder', '__all__', '__author__', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__', '__version__', '_default_decoder', '_default_encoder', 'codecs', 'decoder', 'detect_encoding', 'dump', 'dumps', 'encoder', 'load', 'loads', 'scanner']
    

    In this section, we will learn the following methods:

    • load()
    • loads()
    • dump()
    • dumps()

    Serializing JSON

    Serialization is the technique to convert the Python objects to JSON. Sometimes, computer need to process lots of information so it is good to store that information into the file. We can store JSON data into file using JSON function. The json module provides the dump() and dumps() method that are used to transform Python object.

    Python objects are converted into the following JSON objects. The list is given below:

    Sr. Python Objects JSON
    1. Dict Object
    2. list, tuple Array
    3. Str String
    4. int, float Number
    5. True true
    6. False false
    7. None null
    • The dump() function

    Writing JSON Data into File

    Python provides a dump() function to transmit(encode) data in JSON format. It accepts two positional arguments, first is the data object to be serialized and second is the file-like object to which the bytes needs to be written.

    Let's consider the simple serialization example:

    1. Import json  
    2. # Key:value mapping  
    3. student  = {  
    4. "Name" : "Peter",  
    5. "Roll_no" : "0090014",  
    6. "Grade" : "A",  
    7. "Age"20,  
    8.     "Subject": ["Computer Graphics""Discrete Mathematics""Data Structure"]  
    9. }  
    10.   
    11. with open("data.json","w") as write_file:  
    12.     json.dump(student,write_file)  

    Output:

    {"Name" : "Peter", "Roll_no" : "0090014" , "Grade" : "A", "Age" : 20, "Subject" : ["Computer Graphics", "Discrete Mathematics", "Data Structure"] }
    

    In the above program, we have opened a file named data.json in writing mode. We opened this file in write mode because if the file doesn't exist, it will be created. The json.dump() method transforms dictionary into JSON string.

    • The dumps () function

    The dumps() function is used to store serialized data in the Python file. It accepts only one argument that is Python data for serialization. The file-like argument is not used because we aren't not writing data to disk. Let's consider the following example:

    1. import json  
    2. # Key:value mapping  
    3. student  = {  
    4. "Name" : "Peter",  
    5. "Roll_no" : "0090014",  
    6. "Grade" : "A",  
    7. "Age"20  
    8. }  
    9. b = json.dumps(student)  
    10.   
    11. print(b)  

    Output:

    {"Name": "Peter", "Roll_no": "0090014", "Grade": "A", "Age": 20}
    

    JSON supports primitive data types, such as strings and numbers, as well as nested list, tuples and objects.

    1. import json  
    2.   
    3. #Python  list conversion to JSON  Array   
    4. print(json.dumps(['Welcome'"to""javaTpoint"]))  
    5.   
    6. #Python  tuple conversion to JSON Array   
    7. print(json.dumps(("Welcome""to""javaTpoint")))  
    8.   
    9. # Python string conversion to JSON String   
    10. print(json.dumps("Hello"))  
    11.   
    12. # Python int conversion to JSON Number   
    13. print(json.dumps(1234))  
    14.   
    15. # Python float conversion to JSON Number   
    16. print(json.dumps(23.572))  
    17.   
    18. # Boolean conversion to their respective values   
    19. print(json.dumps(True))  
    20. print(json.dumps(False))  
    21.   
    22. # None value to null   
    23. print(json.dumps(None))   

    Output:

    ["Welcome", "to", "javaTpoint"]
    ["Welcome", "to", "javaTpoint"]
    "Hello"
    1234
    23.572
    true
    false
    null
    

    Deserializing JSON

    Deserialization is the process to decode the JSON data into the Python objects. The json module provides two methods load() and loads(), which are used to convert JSON data in actual Python object form. The list is given below:

    SR. JSON Python
    1. Object dict
    2. Array list
    3. String str
    4. number(int) int
    5. true True
    6. false False
    7. null None

    The above table shows the inverse of the serialized table but technically it is not a perfect conversion of the JSON data. It means that if we encode the object and decode it again after sometime; we may not get the same object back.

    Let's take real-life example, one person translates something into Chinese and another person translates back into English, and that may not be exactly translated. Consider the simple example:

    1. import json  
    2. a = (10,20,30,40,50,60,70)  
    3. print(type(a))  
    4. b = json.dumps(a)  
    5. print(type(json.loads(b)))  

    Output:

    <class 'tuple'>
    <class 'list'>
    
    • The load() function

    The load() function is used to deserialize the JSON data to Python object from the file. Consider the following example:

    1. import json  
    2. # Key:value mapping  
    3. student  = {  
    4. "Name" : "Peter",  
    5. "Roll_no" : "0090014",  
    6. "Grade" : "A",  
    7. "Age"20,  
    8. }  
    9.   
    10. with open("data.json","w") as write_file:  
    11.     json.dump(student,write_file)  
    12.   
    13. with open("data.json""r") as read_file:  
    14.     b = json.load(read_file)  
    15. print(b)  

    Output:

    {'Name': 'Peter', 'Roll_no': '0090014', 'Grade': 'A', 'Age': 20}
    

    In the above program, we have encoded Python object in the file using dump() function. After that we read JSON file using load() function, where we have passed read_file as an argument.

    The json module also provides loads() function, which is used to convert JSON data to Python object. It is quite similar to the load() function. Consider the following example:

    1. Import json  
    2. a = ["Mathew","Peter",(10,32.9,80),{"Name" : "Tokyo"}]  
    3.   
    4. # Python object into JSON   
    5. b = json.dumps(a)  
    6.   
    7. # JSON into Python Object  
    8. c = json.loads(b)  
    9. print(c)  

    Output:

    ['Mathew', 'Peter', [10, 32.9, 80], {'Name': 'Tokyo'}]
    

    json.load() vs json.loads()

    The json.load() function is used to load JSON file, whereas json.loads() function is used to load string.

    json.dump() vs json.dumps()

    The json.dump() function is used when we want to serialize the Python objects into JSON file and json.dumps() function is used to convert JSON data as a string for parsing and printing.

    Python Pretty Print JSON

    Sometimes we need to analyze and debug a large amount of JSON data. It can be done by passing additional arguments indent and sort_keys in json.dumps() and json.dump() methods.

    Note: Both dump() and dumps() functions accept indent and short_keys arguments.

    Consider the following example:

    1. import json  
    2.   
    3. person = '{"Name": "Andrew","City":"English", "Number":90014, "Age": 23,"Subject": ["Data Structure","Computer Graphics", "Discrete mathematics"]}'  
    4.   
    5. per_dict = json.loads(person)  
    6.   
    7. print(json.dumps(per_dict, indent = 5, sort_keys= True))  

    Output:

    {
    "Age": 23,
    "City": "English",
    "Name": "Andrew",
    "Number": 90014,
    "Subject": [
    "Data Structure",
    "Computer Graphics",
    "Discrete mathematics"
    ]
    }
    

    In the above code, we have provided the 5 spaces to the indent argument and the keys are sorted in ascending order. The default value of indent is None and the default value of sort_key is False.

    Encoding and Decoding

    Encoding is the technique for transforming the text or values into an encrypted form. Encrypted data can only be used by the preferred user by decoding it. Encoding is also known as serialization and decoding is also called deserialization. Encoding and decoding are done for JSON(object) format. Python provides a popular package for such operations. We can install it on Windows by the following command:

    1. pip install demjson  

    Encoding - The demjson package provides encode() function that is used to convert the Python object into a JSON string representation. The syntax is given below:

    1. demjson.encode(self,obj,nest_level = 0)  

    Example:1 - Encoding using demjson package

    1. import demjson  
    2. a = [{"Name"'Peter',"Age":20"Subject":"Electronics"}]  
    3. print(demjson.encode(a))  

    Output:

    [{"Age":20,"Name":"Peter","Subject":"Electronics"}]
    

    Decoding-The demjson module provides decode() function, which is used to convert JSON object into Python format type. The syntax is given below:

    1. Import demjson  
    2. a = "['Peter', 'Smith', 'Ricky', 'Hayden']"  
    3. print(demjson.decode(a))  

    Output:

    ['Peter', 'Smith', 'Ricky', 'Hayden']
    

    In this tutorial, we have learned about the Python JSON. JSON is the most effective way to transmit data between the client and the web server.


    Next TopicPython Itertools





    Youtube For Videos Join Our Youtube Channel: Join Now

    Feedback

    • Send your Feedback to feedback@javatpoint.com

    Help Others, Please Share

    facebook twitter pinterest

    Learn Latest Tutorials


    Preparation


    Trending Technologies


    B.Tech / MCA






    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date
    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date

    Python Itertools

    Itertool is one of the most amazing Python 3 standard libraries. This library has pretty much coolest functions and nothing wrong to say that it is the gem of the Python programing language. Python provides excellent documentation of the itertools but in this tutorial, we will discuss few important and useful functions or iterators of itertools.

    The key thing about itertools is that the functions of this library are used to make memory-efficient and precise code.

    Before learning the Python itertools, you should have knowledge of the Python iterator and generators. In this article, we will describe itertools for beginners are well as for professionals.

    Introduction

    According to the official definition of itertools, "this module implements a number of iterator building blocks inspired by constructs from APL, Haskell, and SML." In simple words, the number of iterators can together create 'iterator algebra' which makes it possible to complete the complex task. The functions in itertools are used to produce more complex iterators. Let's take an example: Python built-in zip() function accepts any number of arguments as iterable. It iterates over tuples and return their corresponding elements.

    1. a = [1,2,3]  
    2. b= ['a''b''c']  
    3. c = zip(a,b)  
    4. print(c)  

    Output:

    [(1, 'a'), (2, 'b'), (3, 'c')]
    

    In the above code, we have passed two lists [1,2,3] and ['a', 'b', 'c'] as iterable in zip() function. These lists return one element at a time. In Python, an element that implement .__iter__() or .__getitem__() method called iterable.

    The Python iter() function is used to call on the iterable and return iterator object of the iterable.

    1. a = iter('Hello')  
    2. print(a)  

    Output:

    <str_iterator object at 0x01505FA0>
    

    The Python zip() function calls iter() on each of its argument and then calls next() by combining the result into tuple.

    Note: If you are using the zip() function and map() function that means you are already using itertools. You don't need to import it distinctly.

    Types of Iterator

    There are various types of iterator in itertools module. The list is given below:

    • Infinite iterators
    • Combinatoric iterators
    • Terminating iterators

    Infinite Iterators

    In Python, any object that can implement for loop is called iterators. Lists, tuples, set, dictionaries, strings are the example of iterators but iterator can also be infinite and this type of iterator is called infinite iterator.

    Iterator Argument Results
    count(start,step) start, [step] start, start+step, step+2*step
    cycle() P p0,p1,….plast
    repeat() elem [,n] elem, elem, elem,….endlessly or upto n times
    • count(start, stop): It prints from the start value to infinite. The step argument is optional, if the value is provided to the step then the number of steps will be skipped. Consider the following example:
    1. import itertools  
    2.   
    3. for i in itertools.count(10,5):  
    4.     if i == 50:  
    5.         break  
    6.     else:  
    7.         print(i,end=" ")  

    Output:

    10 15 20 25 30 35 40 45
    
    • cycle(iterable): This iterator prints all value in sequence from the passed argument. It prints the values in a cyclic manner. Consider the following example:
    1. import itertools  
    2. temp = 0  
    3. for i in itertools.cycle("123"):  
    4.     if temp > 7:  
    5.         break  
    6.     else:  
    7.         print(i,end=' ')  
    8.         temp = temp+1  

    Output:

     1 2 3 1 2 3 1 2 3 1 2
    

    Example - 2: Using next() function

    1. import itertools  
    2.   
    3. val = ['Java''T''Point']  
    4.   
    5. iter = itertools.cycle(val)  
    6.   
    7. for i in range(6):  
    8.     # Using next function  
    9.     print(next(iter), end = " ")  

    Output:

    Java T Point Java T Point
    
    • repeat(val,num): As the name suggests, it repeatedly prints the passed value for infinite time. The num argument is optional. Consider the following example:
    1. import itertools  
    2. print("Printing the number repeadtly:")  
    3. print(list(itertools.repeat(40,15)))  

    Output:

    [40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40, 40]
    

    Combinatoric iterators: The complex combinatorial constructs are simplified by the recursive generators. The permutations, combinations, and Cartesian products are the example of the combinatoric construct.

    In Python, there are four types of combinatoric iterators:

    • Product() - It is used to calculate the cartesian product of input iterable. In this function, we use the optional repeat keyword argument for computation of the product of an iterable with itself. The repeat keyword represents the number of repetitions. It returns output in the form of sorted tuples. Consider the following example:
    1. from itertools import product  
    2.   
    3. print("We are computing cartesian product using repeat Keyword Argument:")  
    4. print(list(product([12], repeat=2)))  
    5. print()  
    6.   
    7. print("We are computing cartesian product of the containers:")  
    8. print(list(product(['Java''T''point'], '5')))  
    9. print()  
    10.   
    11. print("We are computing product of the containers:")  
    12. print(list(product('CD', [45])))  

    Output:

    Computing cartesian product using repeat Keyword Argument:
    [(1, 1), (1, 2), (2, 1), (2, 2)]
    
    Computing cartesian product of the containers:
    [('Java', '5'), ('T', '5'), ('point', '5')]
    
    Computing product of the containers:
    [('C', 4), ('C', 5), ('D', 4), ('D', 5)]
    
    • Permutations(): It is used to generate all possible permutation of an iterable. The uniqueness of each element depends upon their position instead of values. It accepts two argument iterable and group_size. If the value of group_size is none or not specified then group_size turns into length of the iterable.
    1. from itertools import permutations  
    2.   
    3. print("Computing all permutation of the following list")  
    4. print(list(permutations([3,"Python"],2)))  
    5. print()  
    6.   
    7. print("Permutations of following string")  
    8. print(list(permutations('AB')))  
    9. print()  
    10.   
    11. print("Permutation of the given container is:")  
    12. print(list(permutations(range(4),2)))  

    Output:

    Computing all permutation of the following list
    [(3, 'Python'), ('Python', 3)]
    
    Permutations of following string
    [('A', 'B'), ('B', 'A')]
    
    Permutation of the given container is:
    [(0, 1), (0, 2), (0, 3), (1, 0), (1, 2), (1, 3), (2, 0), (2, 1), (2, 3), (3, 0), (3, 1), (3, 2)]
    
    • Combinations(): It is used to print all the possible combinations (without replacement) of the container which is passed as argument in the specified group size in sorted order.
    1. from itertools import combinations  
    2. print("Combination of list in sorted order(without replacement)",list(combinations(['B',3],2)))  
    3. print()  
    4.   
    5. print("Combination of string in sorted order",list(combinations("ZX",2)))  
    6. print()  
    7.   
    8. print("Combination of list in sorted order",list(combinations(range(20),1)))  

    Output:

    Combination of list in sorted order(without replacement) [('B', 3)]
    Combination of string in sorted order [('Z', 'X')]
    Combination of list in sorted order [(0,), (1,), (2,), (3,), (4,), (5,), (6,), (7,), (8,), (9,)]
    
    • Combination_with_replacement(): It accepts two arguments, first argument is a r-length tuple and the second argument is repetition. It returns a subsequence of length n from the elements of the iterable and repeat the same process. Separate elements may repeat itself in combination_with_replacement()
    1. from itertools import combinations_with_replacement  
    2.   
    3. print("Combination of string in sorted order(with replacement) is:")  
    4. print(list(combinations_with_replacement("XY"3)))  
    5. print()  
    6.   
    7. print("Combination of list in sorted order(with replacement) is:")  
    8. print(list(combinations_with_replacement([42], 3)))  
    9. print()  
    10.   
    11. print("Combination of container in sorted order(with replacement) is:")  
    12. print(list(combinations_with_replacement(range(3), 2)))  

    Output:

    Combination of string in sorted order(with replacement) is:
    [('X', 'X', 'X'), ('X', 'X', 'Y'), ('X', 'Y', 'Y'), ('Y', 'Y', 'Y')]
    
    Combination of list in sorted order(with replacement) is:
    [(4, 4, 4), (4, 4, 2), (4, 2, 2), (2, 2, 2)]
    
    Combination of container in sorted order(with replacement) is:
    [(0, 0), (0, 1), (0, 2), (1, 1), (1, 2), (2, 2)]
    

    Terminating Iterator

    Terminating iterators are generally used to work on the small input sequence and generate the output based on the functionality of the method used in iterator.

    There are different types of terminating iterator:

    • accumulate(iter, func): It takes two arguments, the first argument is iterable and the second is a function which would be followed at each iteration of value in iterable. If the function is not defined in accumulate() iterator, addition takes place by default. The output iterable depends on the input iterable; if input iterable contains no value then the output iterable will also be empty.
    1. import itertools  
    2. import operator  
    3.   
    4. # initializing list 1  
    5. list1 = [1457911]  
    6.   
    7. # using accumulate() that will prints the successive summation of elements  
    8. print("The sum is : ", end="")  
    9. print(list(itertools.accumulate(list1)))  
    10.   
    11. # using accumulate() that will prints the successive multiplication of elements  
    12. print("The product is : ", end="")  
    13. print(list(itertools.accumulate(list1, operator.mul)))  
    14.   
    15.   
    16. # using accumulate() that will prints the successive summation of elements  
    17. print("The sum is : ", end="")  
    18. print(list(itertools.accumulate(list1)))  
    19.   
    20. # using accumulate() that will prints the successive multiplication of elements  
    21. print("The product is : ", end="")  
    22. print(list(itertools.accumulate(list1, operator.mul)))  

    Output:

    The sum is : [1, 5, 10, 17, 26, 37]
    The product is : [1, 4, 20, 140, 1260, 13860]
    The sum is : [1, 5, 10, 17, 26, 37]
    The product is : [1, 4, 20, 140, 1260, 13860]
    
    • chain(iter1, iter2) - It is used to print all the values in iterable passed in the form of chain and declared in arguments. Consider the following example:
    1. import itertools  
    2.   
    3. # declaring list 1  
    4. list1 = [1234]  
    5.   
    6. # declaring list 2  
    7. list2 = [1568]  
    8.   
    9. # declaring list 3  
    10. list3 = [9101112]  
    11.   
    12. # using chain() function that will to print all elements of lists  
    13. print("The output is : ", end="")  
    14. print(list(itertools.chain(list1, list2, list3)))  

    Output:

    The output is: [1, 2, 3, 4, 1, 5, 6, 8, 9, 10, 11, 12]
    
    • dropwhile(func, seq) - It starts printing the character only after the func. Consider the following argument:
    1. import itertools  
    2. # initializing list  
    3. list1 = [24578]  
    4. # using dropwhile() iterator that will print start displaying after condition is false  
    5. print("The output is : ", end="")  
    6. print(list(itertools.dropwhile(lambda x: x % 2 == 0, list1)))  

    Output:

    The output is : [5, 7, 8]
    
    • filterfalse(func,seq) - We can assume it by its name, as this iterator prints only those values that return false for the passed function. Consider the following example:
    1. import itertools  
    2.   
    3. # declaring list  
    4. list1 = [1214152728]  
    5.   
    6. # using filterfalse() iterator that will print false values  
    7. print("The Output is: ", end="")  
    8. print(list(itertools.filterfalse(lambda x: x % 2 == 0, list1)))  

    Output:

    The Output is : [15, 27]
    
    • islice(iterable,start,stop,step) - It slices the given iterable according to given position. It accepts four arguments respectively and these are iterable, container, starting pos., ending position and step(optional).
    1. import itertools  
    2. # Declaring list  
    3. list1 = [12346573801920]  
    4. # using islice() iterator that will slice the list acc. to given argument  
    5. # starts printing from 3nd index till 8th skipping 2  
    6. print("The sliced list values are : ", end="")  
    7. print(list(itertools.islice(list1, 282)))  

    Output:

    The sliced list values are : [34, 73, 19]
    
    • starmap(func, tuple list) - It takes two arguments; first argument is function and second argument is list which consists element in the form of tuple. Consider the following example.
    1. import itertools  
    2.   
    3. # Declaring list that contain tuple as element  
    4. list1 = [(102015), (184019), (534290), (161227)]  
    5.   
    6. # using starmap() iterator for selection value acc. to function  
    7. # selects max of all tuple values  
    8. print("The values acc. to function are : ", end="")  
    9. print(list(itertools.starmap(max, list1)))  

    Output:

    The values acc. to function are : [20, 40, 90, 27]
    
    • takewhile(func, iterable) - It is visa-versa of dropwhile(). It will print values until it returns false condition. Consider the following example:
    1. import itertools  
    2.   
    3. # Defining a list  
    4. list1 = [2042647781020]  
    5.   
    6. # takewhile() iterator is used  to print values till condition return false.  
    7. print("Print until 1st false value returned : ", end="")  
    8. print(list(itertools.takewhile(lambda x: x % 2 == 0, list1)))  

    Output:

    The list values until false value return : [20, 42, 64]
    
    • tee(iterator, count) - It divides the container into a number of iterators which is defined in the argument. Consider the following example:
    1. import itertools  
    2.   
    3. # Declaring list  
    4. li = [1234567]  
    5.   
    6. # storing list in iterator  
    7. iti = iter(li)  
    8. # using tee() iterator to create a list of iterators  
    9. # Creating list of 3 iterators having similar values.  
    10. it = itertools.tee(iti, 3)  
    11. # It will print object of iterator  
    12. print(it)  
    13. print("The iterators are : ")  
    14. for i in range(02):  
    15.     print(list(it[i]))  

    Output:

    (<itertools._tee object at 0x01B88D88>, <itertools._tee object at 0x01B88DA8>, <itertools._tee object at 0x01B88BA8>)
    The iterators are :
    [1, 2, 3, 4, 5, 6, 7]
    [1, 2, 3, 4, 5, 6, 7]
    
    • zip_longest(iterable1, iterable2, fillval) - It prints the values of iterable alternatively in sequence. If one of the iterable prints all values, remaining values are filled by the values assigned to fill value.
    1. import itertools  
    2. print(" The combined value of iterrables is :")  
    3. print(*(itertools.zip_longest('Java''Tpoint', fillvalue='_')))  

    Output:

    The combined value of iterables is :
    ('J', 'T') ('a', 'p') ('v', 'o') ('a', 'i') ('_', 'n') ('_', 't')
    

    In this tutorial, we have discussed several useful iterators along with itertools.







    Youtube For Videos Join Our Youtube Channel: Join Now

    Feedback

    • Send your Feedback to feedback@javatpoint.com

    Help Others, Please Share

    facebook twitter pinterest

    Learn Latest Tutorials


    Preparation


    Trending Technologies


    B.Tech / MCA






    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date
    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date

    Python Multiprocessing

    In this article, we will learn how we can achieve multiprocessing using Python. We also discuss its advanced concepts.

    What is Multiprocessing?

    Multiprocessing is the ability of the system to run one or more processes in parallel. In simple words, multiprocessing uses the two or more CPU within the single computer system. This method is also capable to allocate the tasks between more than one process.

    Processing units share the main memory and peripherals to process programs simultaneously. Multiprocessing Application breaks into smaller parts and runs independently. Each process is allocated to the processor by the operating system.

    Python provides the built-in package called multiprocessing which supports swapping processes. Before working with the multiprocessing, we must aware with the process object.

    Why Multiprocessing?

    Multiprocessing is essential to perform the multiple tasks within the Computer system. Suppose a computer without multiprocessing or single processor. We assign various processes to that system at the same time.

    It will then have to interrupt the previous task and move to another to keep all processes going. It is as simple as a chef is working alone in the kitchen. He has to do several tasks to cook food such as cutting, cleaning, cooking, kneading dough, baking, etc.

    Therefore, multiprocessing is essential to perform several task at the same time without interruption. It also makes easy to track all the tasks. That is why the concept of multiprocessing is to arise.

    • Multiprocessing can be represented as a computer with more than one central processor.
    • A Multi-core processor refers to single computing component with two or more independent units.

    In the multiprocessing, the CPU can assign multiple tasks at one each task has its own processor.

    Multiprocessing In Python

    Python provides the multiprocessing module to perform multiple tasks within the single system. It offers a user-friendly and intuitive API to work with the multiprocessing.

    Let's understand the simple example of multiple processing.

    Example -

    1. from multiprocessing import Process  
    2.    def disp():  
    3.       print ('Hello !! Welcome to Python Tutorial')  
    4.       if __name__ == '__main__':  
    5.       p = Process(target=disp)  
    6.       p.start()  
    7.       p.join()  

    Output:

    'Hello !! Welcome to Python Tutorial'
    

    Explanation:

    In the above code, we have imported the Process class then create the Process object within the disp() function. Then we started the process using the start() method and completed the process with the join() method. We can also pass the arguments in the declared function using the args keywords.

    Let's understand the following example of the multiprocessing with arguments.

    Example - 2

    1. # Python multiprocessing example  
    2. # importing the multiprocessing module  
    3.   
    4. import multiprocessing  
    5. def cube(n):  
    6.    # This function will print the cube of the given number  
    7.    print("The Cube is: {}".format(n * n * n))  
    8.   
    9. def square(n):  
    10.     # This function will print the square of the given number  
    11.    print("The Square is: {}".format(n * n))  
    12.   
    13. if __name__ == "__main__":  
    14.    # creating two processes  
    15.    process1 = multiprocessing.Process(targetsquareargs=(5, ))  
    16.    process2 = multiprocessing.Process(targetcubeargs=(5, ))  
    17.   
    18.    # Here we start the process 1  
    19.    process1.start()  
    20.    # Here we start process 2  
    21.    process2.start()  
    22.   
    23.    # The join() method is used to wait for process 1 to complete  
    24.    process1.join()  
    25.    # It is used to wait for process 1 to complete  
    26.    process2.join()  
    27.   
    28.    # Print if both processes are completed  
    29.    print("Both processes are finished")  

    Output:

    The Cube is: 125
    The Square is: 25
    Both processes are finished
    

    Explanation -

    In the above example, We created the two functions - the cube() function calculates the given number's cube, and the square() function calculates the square of the given number.

    Next, we defined the process object of the Process class that has two arguments. The first argument is a target that represents the function to be executed, and the second argument is args that represents the argument to be passed within the function.

    1. process1 = multiprocessing.Process(targetsquareargs=(5, ))  
    2. process2 = multiprocessing.Process(targetcubeargs=(5, ))  

    We have used the start() method to start the process.

    1. process1.start()  
    2. process2.start()  

    As we can see in the output, it waits to completion of process one and then process 2. The last statement is executed after both processes are finished.

    Python Multiprocessing Classes

    Python multiprocessing module provides many classes which are commonly used for building parallel program. We will discuss its main classes - Process, Queue and Lock. We have already discussed the Process class in the previous example. Now we will discuss the Queue and Lock classes.

    Let's see the simple example of a get number of CPUs currently in the system.

    Example -

    1. import multiprocessing  
    2. print("The number of CPU currently working in system : ", multiprocessing.cpu_count())  

    Output:

    ('The number of CPU currently woking in system : ', 32)
    

    The above number of CPUs can vary for your pc. For us, the number of cores is 32.

    Python Multiprocessing Using Queue Class

    We know that Queue is important part of the data structure. Python multiprocessing is precisely the same as the data structure queue, which based on the "First-In-First-Out" concept. Queue generally stores the Python object and plays an essential role in sharing data between processes.

    Queues are passed as a parameter in the Process' target function to allow the process to consume data. The Queue provides the put() function to insert the data and get() function to get data from the queues. Let's understand the following example.

    Example -

    1. # Importing Queue Class  
    2.   
    3. from multiprocessing import Queue  
    4.   
    5. fruits = ['Apple', 'Orange', 'Guava', 'Papaya', 'Banana']  
    6. count = 1  
    7. # creating a queue object  
    8. queue = Queue()  
    9. print('pushing items to the queue:')  
    10. for fr in fruits:  
    11.     print('item no: ', count, ' ', fr)  
    12.     queue.put(fr)  
    13.     count += 1  
    14.   
    15. print('\npopping items from the queue:')  
    16. count = 0  
    17. while not queue.empty():  
    18.     print('item no: ', count, ' ', queue.get())  
    19.     count += 1  

    Output:

    pushing items to the queue:
    ('item no: ', 1, ' ', 'Apple')
    ('item no: ', 2, ' ', 'Orange')
    ('item no: ', 3, ' ', 'Guava')
    ('item no: ', 4, ' ', 'Papaya')
    ('item no: ', 5, ' ', 'Banana')
    
    popping items from the queue:
    ('item no: ', 0, ' ', 'Apple')
    ('item no: ', 1, ' ', 'Orange')
    ('item no: ', 2, ' ', 'Guava')
    ('item no: ', 3, ' ', 'Papaya')
    ('item no: ', 4, ' ', 'Banana')
    

    Explanation -

    In the above code, we have imported the Queue class and initialized the list named fruits. Next, we assigned a count to 1. The count variable will count the total number of elements. Then, we created the queue object by calling the Queue() method. This object will used to perform operations in the Queue. In for loop, we inserted the elements one by one in the queue using the put() function and increased the count by 1 with each iteration of loop.

    Python Multiprocessing Lock Class

    The multiprocessing Lock class is used to acquire a lock on the process so that we can hold the other process to execute a similar code until the lock has been released. The Lock class performs mainly two tasks. The first is to acquire a lock using the acquire() function and the second is to release the lock using the release() function.

    Python Multiprocessing Example

    Suppose we have multiple tasks. So, we create two queues: the first queue will maintain the tasks, and the other will store the complete task log. The next step is to instantiate the processes to complete the task. As discussed previously, the Queue class is already synchronized, so we don't need to acquire a lock using the Lock class.

    In the following example, we will merge all the multiprocessing classes together. Let's see the below example.

    Example -

    1. from multiprocessing import Lock, Process, Queue, current_process  
    2. import time  
    3. import queue   
    4.   
    5.   
    6. def jobTodo(tasks_to_perform, complete_tasks):  
    7.     while True:  
    8.         try:  
    9.   
    10.             # The try block to catch task from the queue.  
    11.             # The get_nowait() function is used to  
    12.             # raise queue.Empty exception if the queue is empty.  
    13.   
    14.             task = tasks_to_perform.get_nowait()  
    15.   
    16.         except queue.Empty:  
    17.   
    18.             break  
    19.         else:  
    20.   
    21.                 # if no exception has been raised, the else block will execute  
    22.                 # add the task completion  
    23.                   
    24.   
    25.             print(task)  
    26.             complete_tasks.put(task + ' is done by ' + current_process().name)  
    27.             time.sleep(.5)  
    28.     return True  
    29.   
    30.   
    31. def main():  
    32.     total_task = 8  
    33.     total_number_of_processes = 3  
    34.     tasks_to_perform = Queue()  
    35.     complete_tasks = Queue()  
    36.     number_of_processes = []  
    37.   
    38.     for i in range(total_task):  
    39.         tasks_to_perform.put("Task no " + str(i))  
    40.   
    41.     # defining number of processes  
    42.     for w in range(total_number_of_processes):  
    43.         p = Process(target=jobTodoargs=(tasks_to_perform, complete_tasks))  
    44.         number_of_processes.append(p)  
    45.         p.start()  
    46.   
    47.     # completing process  
    48.     for p in number_of_processes:  
    49.         p.join()  
    50.   
    51.     # print the output  
    52.     while not complete_tasks.empty():  
    53.         print(complete_tasks.get())  
    54.   
    55.     return True  
    56.   
    57.   
    58. if __name__ == '__main__':  
    59.     main()  

    Output:

    Task no 2
    Task no 5
    Task no 0
    Task no 3
    Task no 6
    Task no 1
    Task no 4
    Task no 7
    Task no 0 is done by Process-1
    Task no 1 is done by Process-3
    Task no 2 is done by Process-2
    Task no 3 is done by Process-1
    Task no 4 is done by Process-3
    Task no 5 is done by Process-2
    Task no 6 is done by Process-1
    Task no 7 is done by Process-3
    

    Python Multiprocessing Pool

    Python multiprocessing pool is essential for parallel execution of a function across multiple input values. It is also used to distribute the input data across processes (data parallelism). Consider the following example of a multiprocessing Pool.

    Example -

    1. from multiprocessing import Pool  
    2. import time  
    3.   
    4. w = (["V", 5], ["X", 2], ["Y", 1], ["Z", 3])  
    5.   
    6.   
    7. def work_log(data_for_work):  
    8.     print(" Process name is %s waiting time is %s seconds" % (data_for_work[0], data_for_work[1]))  
    9.     time.sleep(int(data_for_work[1]))  
    10.     print(" Process %s Executed." % data_for_work[0])  
    11.   
    12.   
    13. def handler():  
    14.     p = Pool(2)  
    15.     p.map(work_log, w)  
    16.   
    17. if __name__ == '__main__':  
    18.     handler()  

    Output:

    Process name is V waiting time is 5 seconds
    Process V Executed.
    Process name is X waiting time is 2 seconds
    Process X Executed.
    Process name is Y waiting time is 1 seconds
    Process Y Executed.
    Process name is Z waiting time is 3 seconds
    Process Z Executed.
    

    Let's understand another example of the multiprocessing Pool.

    Example - 2

    1. from multiprocessing import Pool  
    2. def fun(x):  
    3.     return x*x  
    4.   
    5. if __name__ == '__main__':  
    6.     with Pool(5) as p:  
    7.         print(p.map(fun, [1, 2, 3]))  

    Output:

    [1, 8, 27]
    

    Proxy Objects

    The proxy objects are referred to as shared objects which reside in a different process. This object is also called as a proxy. Multiple proxy objects might have a similar referent. A proxy object consists of various methods which are used to invoked corresponding methods of its referent. Below is the example of proxy objects.

    Example -

    1. from multiprocessing import Manager  
    2. manager = Manager()  
    3. l = manager.list([i*i for i in range(10)])  
    4. print(l)  
    5. print(repr(l))  
    6. print(l[4])  
    7. print(l[2:5])  

    Output:

    [0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
    <ListProxy object, typeid 'list' at 0x7f063621ea10>
    16
    [4, 9, 16]
    

    The proxy objects are picklable so we can pass them between processes. These objects are also used for level of control over the synchronization.

    Commonly Used Functions of Multiprocessing

    So far, we have discussed the basic concepts of multiprocessing using Python. Multiprocessing is a broad topic itself and essential for performing various tasks within a single system. We are defining a few essential functions that are commonly used to achieve multiprocessing.

    Method Description
    pipe() The pipe() function returns a pair of connection objects.
    run() The run() method is used to represent the process activities.
    start() The start()method is used to start the process.
    join([timeout]) The join() method is used to block the process until the process whose join() method is called terminates. The timeout is optional argument.
    is_alive() It returns if process is alive.
    terminate() As the name suggests, it is used to terminate the process. Always remember - the terminate() method is used in Linux, for Windows, we use TerminateProcess() method.
    kill() This method is similar to the terminate() but using the SIGKILL signal on Unix.
    close() This method is used to close the Process object and releases all resources associated with it.
    qsize() It returns the approximate size of the queue.
    empty() If queue is empty, it returns True.
    full() It returns True, if queue is full.
    get_await() This method is equivalent get(False).
    get() This method is used to get elements from the queue. It removes and returns an element from queue.
    put() This method is used to insert an element into the queue.
    cpu_count() It returns the number of working CPU within the system.
    current_process() It returns the Process object corresponding to the current process.
    parent_process() It returns the parent Process object corresponding to the current process.
    task_done() This function is used indicate that an enqueued task is completed.
    join_thread() This method is used to join the background thread






    Youtube For Videos Join Our Youtube Channel: Join Now

    Feedback

    • Send your Feedback to feedback@javatpoint.com

    Help Others, Please Share

    facebook twitter pinterest

    Learn Latest Tutorials


    Preparation


    Trending Technologies


    B.Tech / MCA






    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date
    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date

    How to Calculate Distance between Two Points using GEOPY.

    The geopy is a Python library which helps to calculate geographical distance. In this tutorial, we will discuss different methods of how the user can calculate the distance between two places on the earth.

    First, the user has to install the geopy by using the following command:

    1. pip install geopy  

    After successful installation, we are ready to work with the geopy library.

    Calculate Distance between Two Points

    Below are the important methods that used to calculate the distance between two points.

    Method 1: By using Geodesic Distance

    The geodesic distance is the length of the shortest path between two points on any surface of Earth. In the following example, we will show how the user can calculate the Geodesic Distance from the latitude and longitude data.

    Example:

    1. # First, import the geodesic module from the geopy library  
    2. from geopy.distance import geodesic as GD  
    3.     
    4. # Then, load the latitude and longitude data for New York & Texas  
    5. New_York = (40.712874.0060)  
    6. Texas = (31.968699.9018)  
    7.     
    8. # At last, print the distance between two points calculated in kilo-metre  
    9. print ("The distance between New York and Texas is: ", GD(New_York, Texas).km)  

    Output:

    The distance between New York and Texas is: 2507.14797665193
    

    Method 2: By using Great Circle Distance

    The great circle distance is the shortest path between two points on the sphere. In this case, we will assume the earth is the perfect sphere. The following example shows how the user can calculate great circle distance by using longitude and latitude data of two points.

    Example:

    1. # First, import the great_circle module from the geopy library  
    2. from geopy.distance import great_circle as GC  
    3.     
    4. # Then, load the latitude and longitude data for New York & Texas  
    5. New_York = (40.712874.0060)  
    6. Texas = (31.968699.9018)  
    7.     
    8. # At last, print the distance between two points calculated in kilo-metre  
    9. print ("The distance between New York and Texas is: ", GC(New_York, Texas).km)  

    Output:

    The distance between New York and Texas is: 2503.045970189156
    

    Method 3: By using Haversine Formula

    The orthodromic distance is used for calculating the shortest distance between two latitudes and longitudes points on the earth's surface.

    Using this method, the user needs to have the coordinates of two points (P and Q).

    First, they have to convert the values of latitude and longitude points from decimal degrees to radians and then divide the values of latitude and longitude by (180/π). The user should use the value of "π = 22/7". Then, the value of (180/π) will be "57.29577". If the user wants to calculate the distance in miles, they can use the value of the radius of Earth, that is, "3,963". And if the user wants to calculate the distance in Kilo-metre, they can use the value "6,378.80".

    Formulas:

    1. How to calculate the value of latitude in radians:  
    2. The value of Latitude in Radian: Latitude (La1) = La1 / (180/?)  
    3. OR  
    4. The value of Latitude in Radian: Latitude (La1) = La1 / 57.29577  
    5. How to calculate the value of longitude in radians:  
    6. The value of Longitude in Radian: Longitude (Lo1) = Lo1 / (180/?)  
    7. OR  
    8. The value of Longitude in Radian: Longitude (Lo1) = Lo1 / 57.29577  

    The user needs the coordinates of P point and Q points in terms of longitude and latitude, then using the above formula for converting them into radians.

    Now, calculate the distance between two points by using the following formula.

    Formula:

    For miles:

    1. Distance (D) = 3963.0 * arccos[(sin(La1) * sin(La2)) + cos(La1) * cos(La2) * cos(Lo2 - Lo1)]  

    For kilometre:

    1. Distance (D) = 3963.0 * arccos[(sin(La1) * sin(La2)) + cos(La1) * cos(La2) * cos(Lo2 - Lo1)]  

    Thus, the user can calculate the shortest distance between the two given points on Earth by using Haversine Formula.

    Example:

    1. from math import radians, cos, sin, asin, sqrt  
    2. # For calculating the distance in Kilometres   
    3. def distance_1(La1, La2, Lo1, Lo2):  
    4.        
    5.     # The math module contains the function name "radians" which is used for converting the degrees value into radians.  
    6.     Lo1 = radians(Lo1)  
    7.     Lo2 = radians(Lo2)  
    8.     La1 = radians(La1)  
    9.     La2 = radians(La2)  
    10.         
    11.     # Using the "Haversine formula"  
    12.     D_Lo = Lo2 - Lo1  
    13.     D_La = La2 - La1  
    14.     P = sin(D_La / 2)**2 + cos(La1) * cos(La2) * sin(D_Lo / 2)**2  
    15.    
    16.     Q = 2 * asin(sqrt(P))  
    17.       
    18.     # The radius of earth in kilometres.  
    19.     R_km = 6371  
    20.         
    21.     # Then, we will calculate the result  
    22.     return(Q * R_km)  
    23.       
    24.             
    25.        
    26. # driver code  
    27. La1 = 40.7128  
    28. La2 = 31.9686  
    29. Lo1 = -74.0060  
    30. Lo2 = -99.9018  
    31. print ("The distance between New York and Texas is: ", distance_1(La1, La2, Lo1, Lo2), "K.M")  
    32. # For calculating the distance in Miles  
    33. def distance_2(La1, La2, Lo1, Lo2):  
    34.        
    35.     # The math module contains the function name "radians" which is used for converting the degrees value into radians.  
    36.     Lo1 = radians(Lo1)  
    37.     Lo2 = radians(Lo2)  
    38.     La1 = radians(La1)  
    39.     La2 = radians(La2)  
    40.         
    41.     # Using the "Haversine formula"  
    42.     D_Lo = Lo2 - Lo1  
    43.     D_La = La2 - La1  
    44.     P = sin(D_La / 2)**2 + cos(La1) * cos(La2) * sin(D_Lo / 2)**2  
    45.    
    46.     Q = 2 * asin(sqrt(P))  
    47.     # The radius of earth in Miles.  
    48.     R_Mi = 3963  
    49.         
    50.     # Then, we will calculate the result  
    51.     return(Q * R_Mi)  
    52. print ("The distance between New York and Texas is: ", distance_2(La1, La2, Lo1, Lo2), "Miles")  

    Output:

    The distance between New York and Texas is: 2503.04243426357 K.M
    The distance between New York and Texas is: 1556.985899699659 Miles
    

    Conclusion

    In this tutorial, we have discussed various methods for calculating the distance between two points on the earth's surface by using the geopy library. We have shown examples of each method.







    Youtube For Videos Join Our Youtube Channel: Join Now

    Feedback

    • Send your Feedback to feedback@javatpoint.com

    Help Others, Please Share

    facebook twitter pinterest

    Learn Latest Tutorials


    Preparation


    Trending Technologies


    B.Tech / MCA






    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date
    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date

    Gmail API in Python

    In this tutorial, we are going to learn about Gmail API in Python, and we will also learn how we can use Gmail APIs in Python to perform many Gmail operations such as sending an email, searching an email, deleting an email, etc. For this, we will learn to set up Gmail API in our Python script. First, let us have a brief of Gmail API and its basic introduction.

    Gmail APIs

    Gmail is the most popular mail service in today's world, and it is used by almost all of us and many organizations. Over past years, many Gmail features are enhanced with the use of AI, including suggestions while composing emails and security features (detecting fraud and spam emails).

    Gmail API is APIs based on RESTful APIs that allow its users to interact with our Gmail account, and it helps us to use its features using a Python script.

    Prerequisites of Using Gmail APIs in Python

    We must fulfil the following requirements for using Gmail APIs in our Python script:

    • We should have a Python version higher or equal to 2.6.
    • We must have a google account with Gmail service enabled of it.
    • The system must be installed BeautifulSoup library (if not, then we should use 'pip install bsp' syntax in the command terminal to install it in our device).
    • We should have basic knowledge of Google OAuth libraries and Google API client.

    Installation of Required libraries:

    Before enabling the Gmail APIs to use them in our Python script, let's first install the pre-required libraries in our system. To install the pre-required libraries for enabling the Gmail APIs, we should follow the following steps:

    Step 1: Open the command prompt terminal of the system and make sure that our device has an active internet connection.

    Step 2: Write down the following command in the terminal:

    1. pip install --upgrade google-api-python-client google-auth-httplib2 google-auth-oauthlib  

    Now, press enter to start the installation of libraries.

    Gmail API in Python

    As we can see that, the pre-required libraries for enabling Gmail APIs are successfully installed in our system. Now, we can proceed with enabling Gmail APIs part in this tutorial.

    Enabling Gmail APIs in our device

    We have to follow the following given steps to enable Gmail APIs in our device so that we can use these APIs in our Python script:

    Step 1: Creating New Project on Google Cloud console:

    In this step, first, we have to login into the Google cloud console (https://console.cloud.google.com/?pli=1) with our Google account, and then we have to click on 'New Project' to create a new project.

    Gmail API in Python

    If we already have an existing project, then we can also continue with the existing project.

    Step 2: Now, we have to go to the API and services option from the Project menu that we have created.

    Gmail API in Python

    Step 3: Now, we can see the option 'Enable Gmail API and services,' and we have to choose this option to enable Gmail APIs for the project.

    Gmail API in Python

    Step 4: Configuration of Consent screen:

    Now, in this step, we will configure the consent screen of the project we created by clicking on the 'OAuth Consent Screen' option given in the menu. We can only see this option if the consent screen is not already configured.

    Gmail API in Python

    Step 5: Now, we have to enter the application name of our choice and save the project.

    Step 6: Now, click on the credentials option and go to credentials.

    Gmail API in Python

    Step 7: Creating an OAuth Client ID:

    Now, we click on the 'create credentials' option and go to the OAuth Client ID to create it.

    We perform this by following the below sequential procedure to create a new OAuth Client ID for our project:

    • First, we choose the application type as the desktop application for the project.
      Gmail API in Python
    • After that, we enter the application name (can be the same as we have set in the above steps or can be different) and click on the create button.
    • Now, the OAuth client ID will be created for our project, and we download it and save it with the 'credentials.json' name and format for future references.
      Gmail API in Python

    Now, we are done with all the steps of enabling Gmail APIs, and we can start using Gmail APIs in our Python script.

    Note: We have to save the client ID and password so that we can use them in future references if required.

    Importing Necessary Modules

    Now, we have set up all the necessary APIs and we should forward with importing all the necessary modules. Let's see the below example of importing modules.

    Example -

    1. # Importing os and pickle module in program  
    2. import os  
    3. import pickle  
    4. # Creating utils for Gmail APIs  
    5. from googleapiclient.discovery import build  
    6. from google_auth_oauthlib.flow import InstalledAppFlow  
    7. from google.auth.transport.requests import Request  
    8. # Importing libraries for encoding/decoding messages in base64  
    9. from base64 import urlsafe_b64decode, urlsafe_b64encode  
    10. # Importing libraries for dealing with the attachment of MIME types in Gmail  
    11. from email.mime.text import MIMEText  
    12. from email.mime.multipart import MIMEMultipart  
    13. from email.mime.image import MIMEImage  
    14. from email.mime.audio import MIMEAudio  
    15. from email.mime.base import MIMEBase  
    16. from email.mime.multipart import MIMEMultipart  
    17. from mimetypes import guess_type as guess_mime_type  
    18.   
    19. # Request all access from Gmail APIs and project  
    20. SCOPES = ['https://mail.google.com/']  
    21. OurEmailID = 'OurMail@gmail.com' # giving our Gmail Id  
    22.   
    23. # using a default function to authenticate Gmail APIs  
    24. def authenticateGmailAPIs():  
    25.     creds = None  
    26.     # Authorizing the Gmail APIs with tokens of pickles  
    27.     if os.path.exists("token.pickle"): # using if else statement  
    28.         with open("token.pickle""rb") as token:  
    29.             creds = pickle.load(token)  
    30.     # If there are no valid credentials available in device, we will let the user sign in manually  
    31.     if not creds or not creds.valid:  
    32.         if creds and creds.expired and creds.refresh_token:  
    33.             creds.refresh(Request())  
    34.         else:  
    35.             flow = InstalledAppFlow.from_client_secrets_file('client_secret_107196167488-dh4b2pmpivffe011kic4em9a4ugrcooi.apps.googleusercontent.com.json', SCOPES) # downloaded credential name  
    36.             creds = flow.run_local_server(port=0) # running credentials  
    37.         # Save the credentials for the next run  
    38.         with open("token.pickle""wb") as token:  
    39.             pickle.dump(creds, token)  
    40.     return build('Gmail''v1', credentials=creds) # using Gmail to authenticate  
    41.   
    42. # Get the Gmail API service by calling the function  
    43. service = authenticateGmailAPIs()  

    Output:

    Please visit this URL to authorize this application: https://accounts.google.com/o/oauth2/auth?response_type=code&client_id=107196167488-dh4b2pmpivffe011kic4em9a4ugrcooi.apps.googleusercontent.com&redirect_uri=http%3A%2F%2Flocalhost%3A55991%2F&scope=https%3A%2F%2Fmail.google.com%2F&state=kfXlNyjvbKetyUK0op7OF9WY7shrKS&access_type=offline
    

    Gmail API in Python

    Explanation -

    When we run the above given program, we will see an option to choose the browser, as we can see in the above image, and if we don't see an option like this, we need to click on the link given in the output. Then, we can select the browser of our choice or the default browser of the system to continue the process. Now, when we select the browser, we will be redirected to our browser and can see the following tab opened in it:

    Gmail API in Python

    Now, we tick the checkbox option shown in the dialogue box to give the required permissions, and then, we will have to click on the continue option. After clicking on continue, we can see the following window will open in the same tab:

    Gmail API in Python

    As the window is showing, the authentication part for enabling Gmail API is completed, and we have linked our Gmail account with the project for Gmail APIs we created.

    Note: Of course, we have to put our mail that we can connect to Gmail APIs and use for future references for working with Gmail APIs, in the place of 'OurMail@gmail.com' as provided in the above program.

    Performing Actions using Gmail APIs in Python

    Now, we have completely set up and enabled Gmail APIs in our project with Python script. Now, we can perform many actions from our Gmail account with a Python program.

    We can perform the following Gmail actions with our Python script using Gmail APIs in it:

    • Sending an email
    • Searching an email
    • Deleting an email or entire emails history
    • Reading an email
    • Marking read/unread an email etc.

    In this tutorial, we will only about sending an email using Gmail APIs in our Python program, and we will learn to write the code to perform this action with Python script.

    Sending an email

    We can simply write and send an email by writing a Python program and using enabled Gmail APIs in it. Here, in this section, we will write a Python program by which we can send emails from our Gmail account just by running the program.

    Look at the following Python program for a better understanding of it:

    Example -

    1. # importing os and pickle module in program  
    2. import os  
    3. import pickle  
    4. # Creating utils for Gmail APIs  
    5. from googleapiclient.discovery import build  
    6. from google_auth_oauthlib.flow import InstalledAppFlow  
    7. from google.auth.transport.requests import Request  
    8. # Importing libraries for encoding/decoding messages in base64  
    9. from base64 import urlsafe_b64decode, urlsafe_b64encode  
    10. # Importing libraries for dealing with the attachment of MIME types in Gmail  
    11. from email.mime.text import MIMEText  
    12. from email.mime.multipart import MIMEMultipart  
    13. from email.mime.image import MIMEImage  
    14. from email.mime.audio import MIMEAudio  
    15. from email.mime.base import MIMEBase  
    16. from email.mime.multipart import MIMEMultipart  
    17. from mimetypes import guess_type as guess_mime_type  
    18.   
    19. # Request all access from Gmail APIs and project  
    20. SCOPES = ['https://mail.google.com/'] # providing the scope for Gmail APIs  
    21. OurEmailID = 'OurMail@gmail.com' # giving our Gmail Id  
    22.   
    23. # using a default function to authenticate Gmail APIs  
    24. def authenticateGmailAPIs():  
    25.     creds = None  
    26.     # authorizing the Gmail APIs with tokens of pickles  
    27.     if os.path.exists("token.pickle"): # using if else statement  
    28.         with open("token.pickle""rb") as token:  
    29.             creds = pickle.load(token)  
    30.     # if there are no valid credentials available in device, we will let the user sign in manually  
    31.     if not creds or not creds.valid:  
    32.         if creds and creds.expired and creds.refresh_token:  
    33.             creds.refresh(Request())  
    34.         else:  
    35.             flow = InstalledAppFlow.from_client_secrets_file('client_secret_107196167488-dh4b2pmpivffe011kic4em9a4ugrcooi.apps.googleusercontent.com.json', SCOPES) # downloaded credential name  
    36.             creds = flow.run_local_server(port=0) # running credentials  
    37.         # save the credentials for the next run  
    38.         with open("token.pickle""wb") as token:  
    39.             pickle.dump(creds, token)  
    40.     return build('gmail''v1', credentials=creds) # using Gmail to authenticate  
    41.   
    42. # Get the Gmail API service by calling the function  
    43. ServicesGA = authenticateGmailAPIs()  
    44.   
    45. # Using a default funnction to add attachments in Mail  
    46. def AddAttachment(mail, NameofFile):  
    47.     content_type, encoding = guess_mime_type(NameofFile)  
    48.     if content_type is None or encoding is not None: # defining none file type attachment  
    49.         content_type = 'application/octet-stream'  
    50.     main_type, sub_type = content_type.split('/'1)  
    51.     if main_type == 'text': # defining text file type attachment  
    52.         fp = open(NameofFile, 'rb') # opening file  
    53.         msg = MIMEText(fp.read().decode(), _subtype = sub_type)  
    54.         fp.close()   
    55.     elif main_type == 'image': # defining image file type attachment  
    56.         fp = open(NameofFile, 'rb')  
    57.         msg = MIMEImage(fp.read(), _subtype = sub_type)  
    58.         fp.close()  
    59.     elif main_type == 'audio': # defining audio file type attachment  
    60.         fp = open(NameofFile, 'rb')  
    61.         msg = MIMEAudio(fp.read(), _subtype = sub_type) # reading file  
    62.         fp.close()  
    63.     else:  
    64.         fp = open(NameofFile, 'rb')  
    65.         msg = MIMEBase(main_type, sub_type)  
    66.         msg.set_payload(fp.read())  
    67.         fp.close() # closing file  
    68.     NameofFile = os.path.basename(NameofFile)  
    69.     msg.add_header('Content-Disposition''attachment', NameofFile = NameofFile)  
    70.     mail.attach(msg) # composing the mail with given attachment  
    71.   
    72. # Creating mail with a default function  
    73. def CreateMail(RecieverMail, SubofMail, BodyofMail, attachments=[]): # various import content of mail as function's parameter  
    74.     # Using if else to check if there is any attachment in mail or not  
    75.     if not attachments: # no attachment is given in the mail  
    76.         mail = MIMEText(BodyofMail) # Body of Mail  
    77.         mail['to'] = RecieverMail # mail ID of Reciever  
    78.         mail['from'] = OurEmailID # our mail ID  
    79.         mail['subject'] = SubofMail # Subject of Mail  
    80.     else: # attachment is given in the mail  
    81.         mail = MIMEMultipart()  
    82.         mail['to'] = RecieverMail  
    83.         mail['from'] = OurEmailID  
    84.         mail['subject'] = SubofMail  
    85.         mail.attach(MIMEText(BodyofMail))  
    86.         for NameofFile in attachments:  
    87.             AddAttachment(mail, NameofFile)  
    88.     return {'raw': urlsafe_b64encode(mail.as_bytes()).decode()}  
    89.   
    90. # Creating a default function to send a mail  
    91. def SendMail(ServicesGA, RecieverMail, SubofMail, BodyofMail, attachments=[]):  
    92.     return ServicesGA.users().messages().send(  
    93.       userId = "me",  
    94.       body = CreateMail(RecieverMail, SubofMail, BodyofMail, attachments)  
    95.     ).execute() # Body of the mail with execute() function  
    96.   
    97. # Sending an email by adding important content, i.e., Reciever's mail, Subject, Body, etc.  
    98. SendMail(ServicesGA, "Reciever@gmail.com""Python Project i.e., This is the subject of Mail we are sendimg!",   
    99.             "Now, this is the body of the email we are writing and we can add only written text here!", ["test.txt""client_secret_107196167488-dh4b2pmpivffe011kic4em9a4ugrcooi.apps.googleusercontent.com.json"]) # calling out default SendMail() function  

    Output:

    Gmail API in Python

    If we put our mail in the place of the receiver's mail, i.e., Reciever@gmail.com, we will find that the mail is actually sent to the mail we entered as receiver's mail when we run the program, same as what we can see in the above output image.

    Conclusion

    To use the Gmail APIs with our Python script or simply in Python, first, we have to enable them, and create a Project in Google cloud with our Gmail account.

    We can also perform many other actions like reading, deleting, etc., using Gmail APIs in our Python program like sending emails. We can also modify many things into our Gmail account that we authenticated with our Gmail APIs project, just by running our Python scripts (enabled with Gmail APIs).







    Youtube For Videos Join Our Youtube Channel: Join Now

    Feedback

    • Send your Feedback to feedback@javatpoint.com

    Help Others, Please Share

    facebook twitter pinterest

    Learn Latest Tutorials


    Preparation


    Trending Technologies


    B.Tech / MCA






    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date
    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date

    How to Plot the Google Map using folium package in Python

    The folium package is built on the data wrangling strengths of the Python ecosystem and the mapping strengths of the Leaflet.js library of JavaScript language. The user can manipulate their data by using Python and then visualize it by using Leaflet.js map through folium package. Folium package is an easy approach of visualizing the data on Leaflet.js map, which has been manipulated by using Python.

    Required Module and Libraries

    Folium: The user can install the Folium package by using the following command.

    1. pip install folium  

    Geopy: The geopy module of Python makes it easy for Python users to locate the coordinates of landmarks, cities, countries on the earth's surface. For installing the geopy module, the user can use the following command:

    1. pip install geopy  

    After successful installation of the both libraries, we follow the below steps to plot Google map.

    Step 1: Create the Base map

    The user can create the base map by using the following program:

    1. import os  
    2. # First, import folium package  
    3. import folium  
    4. from geopy.geocoders import Nominatim as NT  
    5. # Initialize Nominatim API  
    6. geo_locator = NT(user_agent = "geoapiExercises")  
    7. # write the place  
    8. place_1 = "Yemen"  
    9.   
    10. location_1 = geo_locator.geocode(place_1)  
    11. # now, it will search for the location by using the latitude and longitude, with zoom_start = 15  
    12. user_map1 = folium.Map(location = [location_1.longitude, location_1.latitude],  
    13.                                         zoom_start = 15 )  
    14. # At last, open the base map  
    15. user_map1  

    Output:

    How to Plot the Google Map using folium package in Python

    Step 2: Add a Circular Marker

    The user can mark the area with the circle and popup text by using the following code:

    1. import folium  
    2.    
    3. from geopy.geocoders import Nominatim as NT  
    4.   
    5. geo_locator = NT(user_agent = "geoapiExercises")  
    6.   
    7. place_1 = "Yemen"  
    8.   
    9. location_1 = geo_locator.geocode(place_1)  
    10.   
    11. user_map1 = folium.Map(location = [location_1.longitude, location_1.latitude],  
    12.                                         zoom_start = 15 )  
    13.    
    14. # CircleMarker with radius  
    15. folium.CircleMarker(location = [location_1.longitude, location_1.latitude],  
    16.                     radius = 45, popup = ' YEMEN ').add_to(user_map1)  
    17.    
    18. # Now, open the Map with circular Mark  
    19. user_map1  

    Output:

    How to Plot the Google Map using folium package in Python

    Step 3: Add the simple marker for the parachute style marker with the popup text

    The user can use the following code.

    Example -

    1. import os  
    2. import folium  
    3.    
    4. from geopy.geocoders import Nominatim as NT  
    5.   
    6. geo_locator = NT(user_agent = "geoapiExercises")  
    7.   
    8. place_1 = "Yemen"  
    9.   
    10. location_1 = geo_locator.geocode(place_1)  
    11.   
    12. user_map1 = folium.Map(location = [location_1.longitude, location_1.latitude],  
    13.                                         zoom_start = 15)  
    14. #Now, we will pass the string in popup parameter  
    15. folium.Marker([location_1.longitude, location_1.latitude],  
    16.                popup = ['YEMEN']).add_to(user_map1)  
    17. # now, open the map   
    18. user_map1  

    Output:

    How to Plot the Google Map using folium package in Python

    Step 4: Add the line on the map

    The user can use the following code for adding the line on the map to join the two coordinates.

    Example -

    1. # First, import folium package  
    2. import folium  
    3. import os   
    4. from geopy.geocoders import Nominatim as NT  
    5.   
    6. geo_locator = NT(user_agent = "geoapiExercises")  
    7.   
    8. place_1 = "Aden"  
    9. place_2 = "Yemen"  
    10. location_1 = geo_locator.geocode(place_1)  
    11. location_2 = geo_locator.geocode(place_2)  
    12. user_map1 = folium.Map(location = [location_1.longitude, location_1.latitude],  
    13.                                         zoom_start = 6)  
    14.    
    15. folium.Marker([location_1.longitude, location_1.latitude],  
    16.                popup = ['Aden']).add_to(user_map1)  
    17.    
    18. folium.Marker([location_2.longitude, location_2.latitude],  
    19.               popup = 'Yemen').add_to(user_map1)  
    20.    
    21. # Now, we will add the line on the map by using Polyline method .  
    22. # it will connect both coordinates by the line  
    23.    
    24. folium.PolyLine(locations = [[location_1.longitude, location_1.latitude], [location_2.longitude, location_2.latitude]],  
    25.                 line_opacity = 0.5).add_to(user_map1)  
    26. # now, open the map  
    27. user_map1  

    Output:

    How to Plot the Google Map using folium package in Python

    Explanation

    We used the geopy library to get the latitude and longitude of the location. Then we used the "folium.map" method of the folium package for creating the base of Google Maps.

    In step 2, we used "folium.CircleMarker" for marking the circular mark on the location with the pop-up text. In step 3, we used "folium.Marker" to add a parachute style mark on the mentioned location. In the last step, we used "folium.PolyLine" for joining two marks on two different locations on the map.

    Conclusion

    In this tutorial, we have shown how the user can Plot the Google map and add different required functionalities on the map like a circular mark, parachute mark, pop-up text, and the line joining the two coordinates on the map.







    Youtube For Videos Join Our Youtube Channel: Join Now

    Feedback

    • Send your Feedback to feedback@javatpoint.com

    Help Others, Please Share

    facebook twitter pinterest

    Learn Latest Tutorials


    Preparation


    Trending Technologies


    B.Tech / MCA






    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date
    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date

    Grid Search in Python

    In this tutorial, we will discuss the Grid Search for the purpose of hyperparameter tuning. We will also learn about the working of Grid Search along with the implementation of it in optimizing the performance of the method of Machine Learning (ML).

    Hyperparameter tuning is significant for the appropriate working of the models of Machine Learning (ML). A method like Grid Search appears to be a basic utility for hyperparameter optimization.

    The Grid Search Method considers some hyperparameter combinations and selects the one returning a lower error score. This method is specifically useful when there are only some hyperparameters in order to optimize. However, it is outperformed by other weighted-random search methods when the Machine Learning model grows in complexity.

    So let us begin by understanding Grid Search.

    Understanding Grid Search

    Grid Search is an optimization algorithm that allows us to select the best parameters to optimize the issue from a list of parameter choices we are providing, thus automating the 'trial-and-error' method. Although we can apply it to multiple optimization issues; however, it is most commonly known for its utilization in machine learning in order to obtain the parameters at which the model provides the best accuracy.

    Let us consider that the model accepts the below three parameters in the form of input:

    1. Number of hidden layers [2, 4]
    2. Number of neurons in every layer [5, 10]
    3. Number of epochs [10, 50]

    If we want to try out two options for every parameter input (as specified in square brackets above), it estimates different combinations. For instance, one possible combination can be [2, 5, 10]. Finding such combinations manually would be a headache.

    Now, suppose that we had ten different parameters as input, and we would like to try out five possible values for each and every parameter. It would need manual input from the programmer's end every time we like to alter the value of a parameter, re-execute the code, and keep a record of the outputs for every combination of the parameters.

    Grid Search automates that process, as it accepts the possible value for every parameter and executes the code in order to try out each and every possible combination outputs the result for the combinations and outputs the combination having the best accuracy.

    Installing the required libraries

    Before we start implementing the Grid Search in the Python programming language, let us briefly discuss some of the necessary libraries and frameworks that need to be installed in the system.

    These libraries and frameworks are as follows:

    1. Python 3
    2. NumPy
    3. Pandas
    4. Keras
    5. Scikit-Learn

    They are all quite simple to install. We can use the pip installer in order to install these libraries as shown below:

    1. $ pip install numpy tensorflow pandas scikit-learn keras  

    Note: If any issues arise while executing any package, try reinstalling and referring to each package's official documentation.

    Now, let us begin implementing the Grid Search in Python

    Implementation of Grid Search in Python

    In the following section, we will understand how to implement Grid Search on an actual application. We will simply be executing the code and discuss in-depth regarding the section where Grid Search comes in rather than discussing Machine Learning and Data Pre-processing section.

    So, let's get started.

    We will use the Diet Dataset containing data regarding the height and weight of different people based on various attributes such as gender, age, and type of diet. We can directly import the data from an online resource with the help of the Pandas read_csv() function.

    But before that, we have to import the required packages:

    File: mygrid.py

    1. import sys  
    2. import pandas as pd  
    3. import numpy as np  
    4. from sklearn.model_selection import GridSearchCV, KFold  
    5. from keras.models import Sequential  
    6. from keras.layers import Dense, Dropout  
    7. from keras.wrappers.scikit_learn import KerasClassifier  
    8. from keras.optimizers import Adam  

    Explanation:

    In the above snippet of code, we have imported the required packages and libraries necessary for the project. One can also save the program file and execute it in order to check if the libraries and packages are installed and imported properly.

    Once the packages are imported successfully, we have to use the following snippet of code in order to import the dataset and print the first five rows of it.

    File: mygrid.py

    1. # importing the dataset  
    2. mydf = pd.read_csv("Diet_Dataset.csv")  
    3.   
    4. # printing the first five lines of dataset  
    5. print(mydf.head())  

    Output:

     Person gender Age Height pre.weight Diet weight6weeks
    0 25 41 171 60 2 60.0
    1 26 32 174 103 2 103.0
    2 1 0 22 159 58 1 54.2
    3 2 0 46 192 60 1 54.0
    4 3 0 55 170 64 1 63.3
    

    Explanation:

    In the above snippet of code, we have imported the dataset using the read_csv() of the pandas library and stored it within the mydf variable. We have then printed the first five rows using the head() function along with the mydf variable.

    Now, let us divide the data into the feature and label sets in order to apply the standard scaling on the dataset.

    The snippet of code for the same is shown below:

    File: mygrid.py

    1. # converting dataframe into numpy array  
    2. mydataset = mydf.values  
    3.   
    4. X = mydataset[:, 0:6]  
    5. Y = mydataset[:, 6].astype(int)  
    6.   
    7. # Normalizing the data using sklearn StandardScaler  
    8. from sklearn.preprocessing import StandardScaler  
    9.   
    10. myscaler = StandardScaler().fit(X)  
    11.   
    12. # Transforming and displaying the training data  
    13. X_stdized = myscaler.transform(X)  
    14.   
    15. mydata = pd.DataFrame(X_stdized)  

    Explanation:

    In the above snippet of code, we have converted the pandas dataframe into a NumPy array. We have then imported the StandardScaler module from the sklearn library and use the function to normalize the data. We have then transformed and displayed the training data using the transform() function.

    Now, let us consider the following snippet of code in order to create a simple deep learning model.

    File: mygrid.py

    1. # defining the function to create model  
    2. def create_my_model(learnRate, dropoutRate):  
    3.     # Creating model  
    4.     mymodel = Sequential()  
    5.     mymodel.add(Dense(6, input_dim = 6, kernel_initializer = 'normal', activation = 'relu'))  
    6.     mymodel.add(Dropout(dropoutRate))  
    7.     mymodel.add(Dense(3, input_dim = 6, kernel_initializer = 'normal', activation = 'relu'))  
    8.     mymodel.add(Dropout(dropoutRate))  
    9.     mymodel.add(Dense(1, activation = 'sigmoid'))  
    10.   
    11.     # Compiling the model  
    12.     my_Adam = Adam(learning_rate = learnRate)  
    13.     mymodel.compile(loss = 'binary_crossentropy', optimizer = my_Adam, metrics = ['accuracy'])  
    14.     return mymodel  

    Explanation:

    The following snippet of code has defined a function as create_my_model() accepting two parameters, i.e., learnRate and dropoutRate, respectively. We have then created the model as mymodel using the Sequential() function. We have also used the add() along with the Dense() and Dropout() function. We have then compiled the model using the compile() function.

    As a result, when we execute the code, this will lead to loading the dataset, preprocessing it, and creating a machine learning model. Since we are only interested in understanding the working of Grid Search, we haven't performed the train/test split, and we had fitted the model on the complete dataset.

    Now, we will understand how Grid Search makes the programmer's life easier by optimizing the parameters in the next section.

    Training the Model without Grid Search

    In the snippet of code shown below, we will create a model with the help of parameter values that we decided on randomly or based on our intuition and see how our model performs:

    File: mygrid.py

    1. # Declaring the values of the parameter  
    2. dropoutRate = 0.1  
    3. epochs = 1  
    4. batchSize = 20  
    5. learnRate = 0.001  
    6.   
    7. # Creating the model object by calling the create_my_model function we created above  
    8. mymodel = create_my_model(learnRate, dropoutRate)  
    9.   
    10. # Fitting the model onto the training data  
    11. mymodel.fit(X_stdized, Y, batch_size = batchSize, epochs = epochs, verbose = 1)  

    Output:

    4/4 [==============================] - 41s 14ms/step - loss: 0.9364 - accuracy: 0.0000e+00
    

    Explanation:

    In the above snippet of code, we have declared the values of the parameter, i.e., dropoutRate, epochs, batchSize, and learnRate, respectively. We have then created the model object by calling the create_my_model() function. We have then fitted the model onto the training data.

    As a result, the accuracy we got is 0.0000e+00.

    Optimizing Hyper-parameters using Grid Search

    If we are not using the Grid Search method, we can directly call the fit() function on the model we have created above. But in order to utilize the Grid Search method, we have to pass in few arguments to the create_my_model() function. Moreover, we have to declare the grid with various options to try for every parameter. Let us perform that in different parts.

    First of all, we will try modifying the create_my_model() function in order to accept arguments from the calling function as shown below:

    File: mygrid.py

    1. def create_my_model(learnRate, dropoutRate):  
    2.     # Creating the model  
    3.     mymodel = Sequential()  
    4.     mymodel.add(Dense(6, input_dim = 6, kernel_initializer = 'normal', activation = 'relu'))  
    5.     mymodel.add(Dropout(dropoutRate))  
    6.     mymodel.add(Dense(3, input_dim = 6, kernel_initializer = 'normal', activation = 'relu'))  
    7.     mymodel.add(Dropout(dropoutRate))  
    8.     mymodel.add(Dense(1, activation = 'sigmoid'))  
    9.   
    10.     # Compile the model  
    11.     myadam = Adam(learning_rate = learnRate)  
    12.     mymodel.compile(loss = 'binary_crossentropy', optimizer = myadam, metrics = ['accuracy'])  
    13.     return mymodel  
    14.   
    15. # Creating the model object  
    16. mymodel = KerasClassifier(build_fn = create_my_model, verbose = 1)  

    Explanation:

    In the above snippet of code, we have made some changes to the previous create_my_model function and used the KerasClassifier to create the model object.

    Now, let us implement the algorithm for Grid Search and fit the dataset on it:

    File: mygrid.py

    1. # Defining the arguments that we want to use in Grid Search along  
    2. # with the list of values that we want to try out  
    3. learnRate = [0.0010.020.2]  
    4. dropoutRate = [0.00.20.4]  
    5. batchSize = [102030]  
    6. epochs = [1510]  
    7.   
    8. # Making a dictionary of the grid search parameters  
    9. paramgrid = dict(learnRate = learnRate, dropoutRate = dropoutRate, batch_size = batchSize, epochs = epochs )  
    10.   
    11. # Building and fitting the GridSearchCV  
    12. mygrid = GridSearchCV(estimator = mymodel, param_grid = paramgrid, cv = KFold(random_state = None), verbose = 10)  
    13.   
    14. gridresults = mygrid.fit(X_stdized, Y)  
    15.   
    16. # Summarizing the results in a readable format  
    17. print("Best: " + gridresults.best_score_ + " using " + gridresults.best_params_)  
    18.   
    19. means = gridresults.cv_results_['mean_test_score']  
    20. stds = gridresults.cv_results_['std_test_score']  
    21. params = gridresults.cv_results_['params']  
    22.   
    23. for mean, stdev, param in zip(means, stds, params):  
    24.     print(mean + "(" + stdev + ")" + " with: " + param)  

    Output:

    Best: 0.00347268912077, using {batch_size=10, dropoutRate=0.4, epochs=5, learnRate=0.2}
    

    Explanation:

    The above output shows the parameter combination which yields the best accuracy.

    At last, we can conclude that the Grid Search is quite easy to implement in the Python programming language and saved us a lot of time in human labor. We can list down all the arguments we wanted to tune, declare the values that need to be tested, execute the code, and forget about it. The process is so easy and convenient that it requires less input from the programmer's side. Once the best argument combination has been found, we can utilize that for the final model.







    Youtube For Videos Join Our Youtube Channel: Join Now

    Feedback

    • Send your Feedback to feedback@javatpoint.com

    Help Others, Please Share

    facebook twitter pinterest

    Learn Latest Tutorials


    Preparation


    Trending Technologies


    B.Tech / MCA






    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date
    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date

    Python High Order Function

    As we must be aware of the basic concept of Python functions, we should move forward with some advanced concepts related to Python functions. In this tutorial, we are going to discuss the important aspects of High order functions in Python, like what high order functions are, how we can define them in Python, how we can use them in Python and what the properties of high order functions are.

    Prerequisites:

    To learn about high order functions in Python, we must have basic knowledge of the following concepts:

    First, let's start with the first thing, i.e., High order functions, and understand a basic about them.

    High order functions

    A function that is having another function as an argument or a function that returns another function as a return in the output is called the High order function. High order functions operate with other functions given in the program.

    A fact about the High order function is that a high order function is applicable to both functions as well as to the methods that take a function as their parameter or return a function as the result of them. In Python, this concept of high-order functions is supported with every aspect.

    Properties of High order functions in Python

    Now, in this section, we will discuss some of the important properties of high order functions that are applicable in Python as well.

    • In high order function, we can store a function inside a variable.
    • In high order function, a function can act as an instant of an object type.
    • In high order function, we can return a function as a result of another function.
    • In high order function, we can pass a function as a parameter or argument inside another function.
    • We can store Python high order functions in data structures format such as lists, hash tables, etc.

    High order functions in Python

    Now, in this section, we will talk specifically about the Python high order functions and how we can define them. We will discuss the methods and means by which we will define and use high order functions in our Python program.

    Following are the ways to define High order functions in a Python code that we are going to discuss in this tutorial.

    1. Using functions as objects in High order function
    2. Returning function as a result in high order function
    3. Functions as a parameter for another function
    4. Decorators as high order function

    Now, we will discuss each of the above-given methods in detail and learn about their implementation as high order functions in a Python program.

    Method 1: Using functions as objects in High order function

    In Python, we can even assign a given function to a variable also. This assignment of function into variable will not call the actual function, instead of it will create a reference to the function that is created. Thus, it makes this assignment of assigning a function as a variable object will create a high order function in the program.

    Look at the following example program to learn the implementation of method we discussed above:

    Example -

    1. # a default function to take another function parameter  
    2. def spell(text):  
    3.     # Making text in upper  
    4.     return text.upper()   
    5. # Taking text as user input  
    6. text = input("Enter a text to print it in uppercase and double: ")  
    7. # Spell function with text  
    8. print(spell(text))   
    9. # Assigning variable with the default function  
    10. scream = spell  
    11. # Scream with text variable  
    12. print(scream(text))  

    Output:

    Enter a text to print it in uppercase and double: JavaTPoint
    JAVATPOINT
    JAVATPOINT
    

    Method 2: Functions as a parameter for another function

    Basically, Python functions are like Python objects, and therefore we can use Python functions to pass them as an argument inside another function, and that will create a high order function in the program.

    Look at the following program to understand the implementation of above-given method:

    Example -

    1. # Default function for making text uppercase  
    2. def scream(word):   
    3.     return word.upper()   
    4. # Default function for making text lowercase  
    5. def spell(word):   
    6.     return word.lower()   
    7. # A third function that work as a high order function  
    8. def speak(funct):   
    9.     # Storing the function in a variable in high order function   
    10.     speaking = funct("Hello Python Developers! You are welcomed to JavaTpoint")   
    11.     print(speaking)    
    12. # Printing text in uppercase  
    13. speak(scream)  
    14. # Printing text in lowercase  
    15. speak(spell)  

    Output:

    HELLO PYTHON DEVELOPERS! YOU ARE WELCOMED TO JAVATPOINT
    hello python developers! you are welcomed to javatpoint
    

    Method 3: Returning function as a result in high order function

    We can also return a function as the result of another function as an object, and that makes the function a high order function.

    Look at the following example program to learn the implementation of method we discussed above:

    Example -

    1. # A default function for addition  
    2. def Adding(a):  
    3.     # Nested function with second number   
    4.     def Addition(b):   
    5.             return a + b # addition of two numbers  
    6.     return Addition # Result  
    7.   
    8. # Taking both number variable as user input  
    9. a = int(input("Enter First Number: "))  
    10. b = int(input("Enter Second Number: "))  
    11. # Assigning nested adding function to a variable  
    12. AddVariable = Adding(a)  
    13. # Using variable as high order function  
    14. Result = AddVariable(b)  
    15. # Printing result  
    16. print("Sum of Two numbers given by you is: ", Result)  

    Output:

    Enter First Number: 24
    Enter Second Number: 26
    Sum of Two numbers given by you is: 50
    

    Method 4: Decorators as high order function

    We can use decorators as the high order function as the most commonly used high order function in Python. Decorators in Python allow us to modify the behavior of methods or functions we defined in the program, and it also allows us to wrap a function inside another function to extend the behavior of wrapped or parent function. We can even wrap a function inside another function without even permanently modifying the parent function.

    In Python decorators, a function is taken as an argument for the other function, and then these decorators are called inside the wrapped function. Look at the following exemplar syntax for a decorator defined in a Python program.

    Syntax

    1. # Using a decorator  
    2. @JTP_Decorator  
    3. def Python_Decorator():   
    4.     .  
    5.     .  

    The above syntax for the decorator is equivalent to the following Python code for a high order function.

    1. # Using Python default function as Python decorators  
    2. def Python_Decorator():   
    3.     .  
    4.     .  
    5. Python_Decorator = @JTP_Decorator(Python_Decorator)  

    We have referred @JTP_Decorator as a callable function inside the default Python_Decorator() function in the above-given code. We will have to add just some extra code in this structure, and we will get the output as the wrapper function.

    Look at the following program to understand the implementation of above given method:

    Example -

    1. # Using default function as Python decorators  
    2. def Python_Decorator(funct):  
    3.        # Inner nested function  
    4.        def inner():   
    5.               print("This line of code will be printed before the execution of high order function")  
    6.               funct()   
    7.               print("This line of code will be printed after the execution of high order function")  
    8.        return inner   
    9. # A default function as decorator  
    10. def JTP_Decorator():   
    11.     print("This line of code will be printed inside the execution of high order function")  
    12. JTP_Decorator = Python_Decorator(JTP_Decorator) # Python decorator as high order function   
    13. # Python decorator calling out as high order function   
    14. JTP_Decorator()  

    Output:

    This line of code will be printed before the execution of high order function
    This line of code will be printed inside the execution of high order function
    This line of code will be printed after the execution of high order function
    






    Youtube For Videos Join Our Youtube Channel: Join Now

    Feedback

    • Send your Feedback to feedback@javatpoint.com

    Help Others, Please Share

    facebook twitter pinterest

    Learn Latest Tutorials


    Preparation


    Trending Technologies


    B.Tech / MCA






    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date
    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date

    nsetools in Python

    In the following tutorial, we will discuss the nsetools library in the Python programming language. We will understand its features and work with some examples.

    So, let's get started.

    Understanding the nsetools library

    NSE or National Stock Exchange of India Limited is the leading stock exchange of India, situated in Mumbai, Maharashtra. NSE was established in the year 1992 as the first dematerialized electronic exchange in the country.

    Python offers a library that allows the programmers to collect real-time data from National Stock Exchange (India). This library is known as nsetools. We can use this library in different projects, which requires fetching live quotes for a provided index or stock or creating large sets of data for further data analytics. We can also create Command-Line Interface (CLI) Applications that may deliver us the details of the live market at a blazing fast speed, pretty faster than any web browser. The data accuracy is only as correct as provided on the official website of the National Stock Exchange of India Limited. (http://www.nseindia.com)

    Main features of the Python nsetools library

    Some of the key features of the Python nsetools library are stated as follows:

    1. The nsetools library works out of the box, without any setup requirement.
    2. This library helps programmers to fetch livestock code and index codes at blazing fast speed.
    3. It also offers a set of all stocks and indices traded on the National Stock Exchange.
    4. Moreover, it also provides a set of:
      1. Top losers
      2. Top gainers
      3. Most active
    5. It also delivers several helpful Application Programming Interfaces (APIs) in order to validate a stock code and index code.
    6. The library optionally returns data in JSON format.
    7. It has a hundred per cent Unit test coverage.

    How to install the Python nsetools library?

    The installation part of the nsetools library is quite easy, and it has no external dependencies. All the dependencies of the library are part of standard distribution packages of Python. We can install the nsetools library using the pip installer as shown in the following syntax:

    Syntax:

    1. $ pip install nsetools  

    Updating the library

    If some of us already have installed the nsetools library in their systems, then the following command will allow them to update the library.

    Syntax:

    1. $ pip install nsetools -upgrade  

    Python 3 support

    Python 3 support for the library has been included from version 1.0.0 and so on. Now, this library is able to work for both Python 2 as well as Python 3.

    Creating an NSE object

    We can create an NSE object using the Nse() function offered by the nsetools library. The same can be seen in the following example:

    Example:

    1. # importing the Nse() function from the nsetools library  
    2. from nsetools import Nse  
    3.   
    4. # creating an NSE object  
    5. nse_obj = Nse()  
    6.   
    7. # printing the value of the object  
    8. print("NSE Object:", nse_obj)  

    Output:

    NSE Object: Driver Class for National Stock Exchange (NSE)
    

    Explanation:

    In the above snippet of code, we have imported the required function from the library. We have then defined a variable that uses the Nse() function to create an NSE object. We have then printed the value of the variable for the users.

    Getting Information using the nsetools library

    Let us consider an example demonstrating the use of nsetools for gathering Information.

    Example:

    1. # importing the Nse() function from the nsetools library  
    2. from nsetools import Nse  
    3.   
    4. # creating an NSE object  
    5. nse_obj = Nse()  
    6.   
    7. # getting quotation of the company  
    8. the_quotation = nse_obj.get_quote('sbin')  
    9.   
    10. # printing the name of the company  
    11. print(the_quotation["companyName"])  
    12.   
    13. # printing average price  
    14. print("Average Price: " + str(the_quotation["averagePrice"]))  

    Output:

    State Bank of India
    Average Price: 431.97
    

    Explanation:

    In the above snippet of code, we have imported the required module and created an NSE object using the Nse() function. We have then defined another variable that uses the get_quote() function on the NSE object to get the quotation of the specified company. We have then printed the required details for the users.







    Youtube For Videos Join Our Youtube Channel: Join Now

    Feedback

    • Send your Feedback to feedback@javatpoint.com

    Help Others, Please Share

    facebook twitter pinterest

    Learn Latest Tutorials


    Preparation


    Trending Technologies


    B.Tech / MCA






    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date
    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date

    Python program to find the nth Fibonacci Number

    In the following tutorial, we will understand how to find the nth Fibonacci Number using Python. We can define a Fibonacci Number, where the following number is the sum of the preceding two numbers.

    The first two elements of the Fibonacci series are 0 and 1, respectively. We can calculate the third element of the series by adding the preceding two elements and will get the third term as 0 + 1, which is equal to 1. Similarly, the fourth term will be the sum of the second and third terms, which is 1 + 1 = 2 and so on. The series of such numbers is known as a Fibonacci Series.

    The recurrence relation defines a Fibonacci number as shown below:

    Fn = Fn - 1 + Fn - 2

    There are different ways to find the nth Fibonacci Number using the Python programming language. Some of them are as follows:

    1. Finding nth Fibonacci Number using Recursion
    2. Finding nth Fibonacci Number using dynamic programming
    3. Finding nth Fibonacci Number using dynamic programming and space optimization
    4. Finding nth Fibonacci Number using arrays

    Of these ways, the two most fundamental are the Recursion method and the Dynamic method.

    Let us understand the working of these methods in detail with examples.

    Finding nth Fibonacci Number using Recursion

    The term recursion is used to define something within itself. In a programming language like Python, Recursion refers to the process of a function calling itself. With the proper and correct code, the Recursion will create a finite loop.

    Let us consider the following snippet of code for better understanding.

    Example:

    1. # defining the function for Fibonacci Series  
    2. def Fibonacci_Series(n):   
    3.     # using if-else conditional statement  
    4.     if n < 0:  
    5.         print("Oops! Incorrect input")  
    6.     # First Fibonacci number is 0  
    7.     elif n == 0:   
    8.         return (0)   
    9.     # Second Fibonacci number is 1   
    10.     elif n == 1:  
    11.         return (1)  
    12.     else:  
    13.         return (Fibonacci_Series(n - 1) + Fibonacci_Series(n - 2))   
    14. # printing the 12th element of the Fibonacci Series  
    15. print("12th Element of the Fibonacci Series:", Fibonacci_Series(12))  

    Output:

    12th Element of the Fibonacci Series: 144
    

    Explanation:

    In the above snippet of code, we have defined a function as Fibonacci_Series() that accepts a parameter as n.

    Moreover, we are aware that the first two elements of the Fibonacci are 0 and 1. In the event of the input as n = 1 or n = 2 (First or Second terms of Fibonacci series), we have used the if-else conditional statement to return 0 or 1. In case the value of n is greater than 2, the function will call itself with a lower input value. As we can observe that the code returns (Fibonacci_Series(n - 1) + Fibonacci_Series(n - 2)). Here, the function calls itself with a lower value unless it reaches the base value of n = 1 and n = 2, and as we know from before, n = 1 returns 0 and n = 2 returns 1. The returned values are then continuously added to produce the sequence of the Fibonacci Series.

    Finding the nth Fibonacci Number using Dynamic Programming

    Dynamic Programming utilizes Recursion as well; however, it mainly utilizes if-else conditional statements. Within the statements, the value of the Fibonacci number is stored in a variable. With the help of Recursion, the repeated addition allows us to obtain this Fibonacci number.

    Let us consider the following example to understand the same.

    Example:

    1. # defining the function to find the nth Fibonacci Number  
    2. def Fibonacci_series(x):  
    3.     # Taking First two terms of the Fibonacci Series as 0 and 1  
    4.     fib_Array = [01]  
    5.     # Here, as we know that the first two terms of Fibonacci Series are 0 and 1,  
    6.     # we append the remaining values (Fibonacci numbers from index 2 to x)  
    7.     # in the array using recursion and return the last element.   
    8.     # In the range function, we take range(2, x + 1) instead of range(2, x).  
    9.     # This is because range function in python iterates until the value  
    10.     # before the upper limit. So, if we take from 2 to x, it would only  
    11.     # iterate from second to (x - 1)th element.  
    12.     for n in range(2, x + 1):  
    13.         fib_Array.append(fib_Array[n - 1] + fib_Array[n - 2])  
    14.     return fib_Array[x]  
    15. print("12th Term of Fibonacci Series:", Fibonacci_series(12))  

    Output:

    12th Term of Fibonacci Series: 144
    

    Explanation:

    In the above snippet of code, we defined the function as Fibonacci_series(), which accepts the parameter as variable x. We created a one-dimensional array as fib_Array with data elements 0 and 1 in its zeroth and first indices. Then, if the provided input ('x') is less than or equal to 2, which is also the length of the array fib_Array, it returns 0 as the first number for x = 1 and 1 as the second number for x = 2. If the value of x is greater than 2, we have used recursion to call and insert the preceding two data elements. However, rather than returning the nth Fibonacci number directly, we append each of the summated elements to the fib_Array array. At last, we have returned the last element of the array (i.e., the nth element) and printed the value for the users.

    Finding the nth Fibonacci Number using Dynamic Programming and Space Optimization

    This method is almost completely identical to Dynamic Programming. However, dynamic programming utilizes recursion to accomplish recurring addition, whereas this method utilizes the for-loop.

    Let us consider the following example to understand the same.

    Example:

    1. # defing the function to return the nth element of the Fibonacci Series  
    2. def Fibonacci_series(x):   
    3.     # assiging the variables  
    4.     m = 0  
    5.     n = 1  
    6.     # using the if-elif-else conditional statements  
    7.     if x < 0:  
    8.         print("Wrong input")   
    9.     elif x == 0:  
    10.         return m   
    11.     elif x == 1:   
    12.         return n  
    13.     else:  
    14.         # using the for-loop   
    15.         for i in range(2, x + 1):   
    16.             o = m + n  
    17.             m = n   
    18.             n = o   
    19.         return n   
    20. # printing the twelveth term of the Fibonacci Series  
    21. print("12th element of the Fibonacci Series:", Fibonacci_series(12))  

    Output:

    12th element of the Fibonacci Series: 144
    

    Explanation:

    In the above snippet of code, we have defined a function and assigned two variables, m = 0 and n = 1. These elements are the first and second elements of the Fibonacci Series. We have then used the if-elif-else conditional statements where the program returns 0 for input value x = 1 and 1 for input value x = 2. If the value of x is greater than 2, we have used the for-loop of i in the range (2, x + 1). We have taken a variable o to store the sum of the preceding two elements in the series. Once o takes the value of m + n, the value of m is reassigned to n. Subsequently, the value of n is reassigned to the value of o. This process continues, and value 3 keeps reassigning until the loop terminates. Once the loop is terminated, the function returns the value of n, which stores the value of the nth Fibonacci Number.

    Finding the nth Fibonacci Number using Array

    In this method, we create an array of size x by repeated addition using the for-loop. Hence, the nth Fibonacci Number is returned.

    Let us consider the following example to understand the same.

    Example:

    1. # defining the function  
    2. def Fibonacci_series(x):   
    3.   # creating an array in the function  
    4.    fib_Array = [0] * (x + 1)  
    5.    fib_Array[1] = 1  
    6.    # adding elements of the series to the array using addition of previous two elements.  
    7.    for n in range (2, x + 1):  
    8.       fib_Array[n] = fib_Array[n - 1] + fib_Array[n - 2]   
    9.    return fib_Array[x]  
    10. if __name__ == "__main__":  
    11.    print("12th element of the Fibonacci series:", Fibonacci_series(12))  

    Output:

    12th element of the Fibonacci series: 144
    

    Explanation:

    In the above snippet of code, we have defined the function. Within the function, we have created an array to find the nth element of the Fibonacci Series. We have then used the for-loop to add elements of the series to the array by repeating the addition of the preceding two elements. At last, the nth element is returned and printed for the users.







    Youtube For Videos Join Our Youtube Channel: Join Now

    Feedback

    • Send your Feedback to feedback@javatpoint.com

    Help Others, Please Share

    facebook twitter pinterest

    Learn Latest Tutorials


    Preparation


    Trending Technologies


    B.Tech / MCA






    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date
    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date

    Python OpenCV object detection

    OpenCV is the huge and open-source library for image processing, machine learning and computer vision. It is also playing an important role in real-time operation. With the help of the OpenCV library, we can easily process the images as well as videos to identify the objects, faces or even handwriting of a human present in the file. We will only focus to object detection from images using OpenCV in this tutorial. We will learn about how we can use OpenCV to do object detection from a given image using a Python program.

    Object Detection

    Basically, object detection is a modern computer technology that is related to image processing, deep learning and computer vision to detect the objects present in an image file. All the technologies used in the Object detection technique (as we mentioned earlier) deals with detecting instances of the object in the image or video.

    Object Detection using OpenCV

    We have learned about object detection in the previous section, and in this section, we will learn that how we can do object detection in an image or video using the OpenCV library. We will first import the OpenCV library in the Python program, and then we will use functions to perform object detection on an image file given to us. But, before using and importing the library functions, let's first install the requirements for using the Object detection technique.

    In this tutorial, we will use the Haar cascade technique to do object detection. Let's learn in brief about the Haar cascade technique first.

    Haar cascade:

    Basically, the Haar cascade technique is an approach based on machine learning where we use a lot of positive and negative images to train the classifier to classify between the images. Haar cascade classifiers are considered as the effective way to do object detection with the OpenCV library. Now, let's understand the concept of positive and negative images that we have discussed earlier:

    • Positive images: These are the images that contain the objects which we want to be identified from the classifier.
    • Negative Images: These are the images that do not contain any object that we want to be detected by the classifier, and these can be images of everything else.

    Requirements for object detection with Python OpenCV:

    We have to install first some important libraries in our system as it is an important requirement for doing object detection tasks. We have to install the following libraries into our system as the requirement for performing object detection:

    1. Installation of OpenCV library:

    First and foremost, the requirement to perform object detection using the OpenCV library is that the OpenCV library should be present in our device so that we can import it into a Python program and use its object detection functions. If this library is not present in our system, we can use the following command in our command prompt terminal to install it:

    1. pip install opencv-python  

    Python OpenCV object detection

    When we press the enter key after writing this command in the terminal, the pip installer in the command prompt will start installing the OpenCV library into our system.

    Python OpenCV object detection

    As we can see that, the OpenCV library is successfully installed in our system, and now we can import it into a Python program to use its functions.

    2. Installation of matplotlib library:

    Matplotlib is very helpful in the opening, closing, reading etc., images in a Python program, and that's why the installation of this library for object detection becomes an important requirement. If the matplotlib library is not present in our system, we have to use the following command in our command prompt terminal to install it:

    1. pip install matplotlib  

    Python OpenCV object detection

    When we press the enter key after writing this command in the terminal, the pip installer in the command prompt will start installing it into our system.

    Python OpenCV object detection

    As we can see that, the matplotlib library is successfully installed in our system, and now we can import it into a Python program to use its functions for opening, reading etc., images.

    We have installed all the required libraries for performing object detection, and now we can move ahead with the implementation part of this task.

    Implementation of Object detection in Python:

    In this part, we will write the Python programs to do the object detection and understand the implementation of it. We will use the following image in our Python program to perform the object detection on it:

    Python OpenCV object detection

    Opening the Image

    We will first open the image given above and create the environment of the picture to show it in the output. Let's first look at an example program to understand the implementation, and then we will look at the explanation part.

    Example 1: Opening the image using OpenCV and matplotlib library in a Python program:

    1. # Import OpenCV module  
    2. import cv2  
    3. # Import pyplot from matplotlib as pltd  
    4. from matplotlib import pyplot as pltd  
    5. # Opening the image from files  
    6. imaging = cv2.imread("opencv-od.png")  
    7. # Altering properties of image with cv2  
    8. img_gray = cv2.cvtColor(imaging, cv2.COLOR_BGR2GRAY)  
    9. imaging_rgb = cv2.cvtColor(imaging, cv2.COLOR_BGR2RGB)  
    10. # Plotting image with subplot() from plt  
    11. pltd.subplot(111)  
    12. # Displaying image in the output  
    13. pltd.imshow(imaging_rgb)  
    14. pltd.show()  

    Output:

    Python OpenCV object detection

    Explanation:

    First, we have imported the OpenCV (as cv2) and matplotlib (as plt) libraries into the program to use their functions in the code. After that, we have opened the image file using the imread() function of cv2.

    Then, we have defined the properties for the image we opened in the program using the cv2 functions. Then, we subplot the image using the subplot() function of plt and giving parameters in it. In last, we have used the imshow() and show() function of the plt module to show the image in the output.

    As we can see in the output, the image is displayed as a result of the program, and its borders have been sub-plotted.

    Recognition or object detection in the image

    Now, we will use the detectMultiScale() in the program to detect the object present in the image. Following is the syntax for using detectMultiScale() function in the code:

    1. found = xml_data.detectMultiScale(img_gray,   
    2.                                    minSize = (3030))  

    We will use a condition statement with this function in the program to check if any object from the image is detected or not and highlight the detected part. Let's understand the implementation of object detection in the image through an example program.

    Example 2: Object detection in the image using the detectMultiScale() in the following Python program:

    1. # Import OpenCV module  
    2. import cv2  
    3. # Import pyplot from matplotlib as plt  
    4. from matplotlib import pyplot as pltd  
    5. # Opening the image from files  
    6. imaging = cv2.imread("opencv-od.png")  
    7. # Altering properties of image with cv2  
    8. imaging_gray = cv2.cvtColor(imaging, cv2.COLOR_BGR2GRAY)  
    9. imaging_rgb = cv2.cvtColor(imaging, cv2.COLOR_BGR2RGB)  
    10. # Importing Haar cascade classifier xml data  
    11. xml_data = cv2.CascadeClassifier('XML-data.xml')  
    12. # Detecting object in the image with Haar cascade classifier   
    13. detecting = xml_data.detectMultiScale(imaging_gray,   
    14.                                    minSize = (3030))  
    15. # Amount of object detected  
    16. amountDetecting = len(detecting)  
    17. # Using if condition to highlight the object detected  
    18. if amountDetecting != 0:  
    19.     for (a, b, width, height) in detecting:  
    20.         cv2.rectangle(imaging_rgb, (a, b), # Highlighting detected object with rectangle  
    21.                       (a + height, b + width),   
    22.                       (02750), 9)  
    23. # Plotting image with subplot() from plt  
    24. pltd.subplot(111)  
    25. # Displaying image in the output  
    26. pltd.imshow(imaging_rgb)  
    27. pltd.show()  

    Output:

    Python OpenCV object detection

    Explanation:

    After opening the image in the program, we have imported the cascade classifier XML file into the program. Then, we used the detectMultiScale() function with the imported cascade file to detect the object present in the image or not.

    We used if condition in the program to check that object is detected or not, and if the object is detected, we have highlighted the detected object part using for loop with cv2 functions. After highlighting the detected object part in the image, we have displayed the processed image using the plt show() and imshow() function.

    As we can see in the output, the image with the object detected part as highlighted is shown to us when we run the program.







    Youtube For Videos Join Our Youtube Channel: Join Now

    Feedback

    • Send your Feedback to feedback@javatpoint.com

    Help Others, Please Share

    facebook twitter pinterest

    Learn Latest Tutorials


    Preparation


    Trending Technologies


    B.Tech / MCA






    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date
    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date

    Python SimpleImputer module

    In this tutorial, we are going to learn about the SimpleImputer module of the Sklearn library, and it was previously known as impute module but updated in the latest versions of the Sklearn library. We will discuss the SimpleImputer class and how we can use it to handle missing data in a dataset and replace the missing values inside the dataset using a Python program.

    SimpleImputer class

    A scikit-learn class that we can use to handle the missing values in the data from the dataset of a predictive model is called SimpleImputer class. With the help of this class, we can replace NaN (missing values) values in the dataset with a specified placeholder. We can implement and use this module class by using the SimpleImputer() method in the program.

    Syntax for SimpleImputer() method:

    To implement the SimpleImputer() class method into a Python program, we have to use the following syntax:

    1. SimpleImputer(missingValues, strategy)  

    Parameters: Following are the parameters which has to be defined while using the SimpleImputer() method:

    1. missingValues: It is the missing values placeholder in the SimpleImputer() method which has to be imputed during the execution, and by default, the value for missing values placeholder is NaN.
    2. strategy: It is the data that is going to replace the missing values (NaN values) from the dataset, and by default, the value method for this parameter is 'Mean'. The strategy parameter of the SimpleImputer() method can take 'Mean', 'Mode', Median' (Central tendency measuring methods) and 'Constant' value input in it.
    3. fillValue: This parameter is used only in the strategy parameter if we give 'Constant' as replacing value method. We have to define the constant value for the strategy parameter, which is going to replace the NaN values from the dataset.

    SimpleImputer class is the module class of Sklearn library, and to use this class, first we have to install the Sklearn library in our system if it is not present already.

    Installation of Sklearn library:

    We can install the Sklearn by using the following command inside the command terminal prompt of our system:

    1. pip install sklearn  

    After pressing the enter key, the sklearn module will start installing in our device, as we can see below:

    Python SimpleImputer module

    Now, the Sklearn module is installed in our system, and we can move ahead with the SimpleImputer class function.

    Handling NaN values in the dataset with SimpleImputer class

    Now, we will use the SimpleImputer class in a Python program to handle the missing values present in the dataset (that we will use in the program). We will define a dataset in the example program while giving some missing values in it, and then we use the SimpleImputer class method to handle those values from the dataset by defining its parameters. Let's understand the implementation of this through an example Python program.

    Example 1: Look at the following Python program with a dataset having NaN values defined in it:

    1. # Import numpy module as nmp  
    2. import numpy as nmp  
    3. # Importing SimpleImputer class from sklearn impute module  
    4. from sklearn.impute import SimpleImputer  
    5. # Setting up imputer function variable  
    6. imputerFunc = SimpleImputer(missing_values = nmp.nan, strategy ='mean')  
    7. # Defining a dataset  
    8. dataSet = [[32, nmp.nan, 3447], [17, nmp.nan, 7153], [1929, nmp.nan, 79], [nmp.nan, 312337], [19, nmp.nan, 7953]]  
    9. # Print original dataset  
    10. print("The Original Dataset we defined in the program: \n", dataSet)  
    11. # Imputing dataset by replacing missing values  
    12. imputerFunc = imputerFunc.fit(dataSet)  
    13. dataSet2 = imputerFunc.transform(dataSet)  
    14. # Printing imputed dataset  
    15. print("The imputed dataset after replacing missing values from it: \n", dataSet2)  

    Output:

    The Original Dataset we defined in the program:
    [[32, nan, 34, 47], [17, nan, 71, 53], [19, 29, nan, 79], [nan, 31, 23, 37], [19, nan, 79, 53]]
    The imputed dataset after replacing missing values from it:
    [[32. 30. 34. 47. ]
    [17. 30. 71. 53. ]
    [19. 29. 51.75 79. ]
    [21.75 31. 23. 37. ]
    [19. 30. 79. 53. ]]
    

    Explanation:

    We have firstly imported the numpy module (to define a dataset) and sklearn module (to use the SimpleImputer class method) into the program. Then, we defined the imputer to handle the missing values using the SimpleImputer class method, and we used the 'mean' strategy to replace the missing values from the dataset. After that, we have defined a dataset in the program using the numpy module function and gave some missing values (NaN values) in the dataset. Then, we printed the original dataset in the output. After that, we have imputed and replaced the missing values from the dataset with the imputer that we have defined earlier in the program with SimpleImputer class. After imputing the dataset and replacing the missing values from it, we have printed the new dataset as a result.

    As we can see in the output, the imputed value dataset having mean values in the place of missing values, and that's how we can use the SimpleImputer module class to handle NaN values from a dataset.

    Conclusion

    We have read about the SimpleImputer class method in this method, and we learned how we could use it to handle the NaN values present in a dataset. We learned about the strategy value parameter, which we use to define the method for replacing the NaN values of the dataset. We have also learned about the installation of the Sklearn library, and then last, we used the SimpleImputer class method in an example to impute the dataset.







    Youtube For Videos Join Our Youtube Channel: Join Now

    Feedback

    • Send your Feedback to feedback@javatpoint.com

    Help Others, Please Share

    facebook twitter pinterest

    Learn Latest Tutorials


    Preparation


    Trending Technologies


    B.Tech / MCA






    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date
    Javatpoint Logo
    Sort by:
    Relevance
    Relevance
    Date

    Second Largest Number in Python

    When we have a lot of elements in our list, the thought of finding the highest or lowest element can come to our mind and Python has made it much easier for us.

    In this article, we shall how we can use to find the second largest number in Python from a list.

    1. Sorting the list and then print the second last number.
    2. Removing the maximum element.
    3. Finding the maximum element.
    4. Traversing the list.

    Let us have a look at the first approach-

    Sorting the list and then print the second last number

    The following program illustrates how we can do it in Python-

    Example -

    1. #program to find the second largest number of list  
    2. # declaring the list  
    3. list_val = [2030402510]  
    4. # sorting the list  
    5. list_val.sort()  
    6. #displaying the second last element of the list  
    7. print("The second largest element of the list is:", list_val[-2])  

    Output:

    The second largest element of the list is: 30
    

    It's time to go for the explanation part-

    1. We have declared the list from which we want to take out the second last element.
    2. After this, we used the sort method so that all the elements of our list are arranged in ascending order.
    3. Now we make use of negative indexing since the second-largest number will come at the second last position.

    The second method is to obtain the second largest element of the list by removing the maximum element.

    Let us see how we can do it.

    Removing the maximum element

    Example -

    1. #program to find the second largest number of list  
    2.   
    3. # declaring the list  
    4. list_val = [2030402510]  
    5.   
    6. # new_list is a set of list1  
    7. res_list = set(list_val)  
    8.   
    9. #removing the maximum element  
    10. res_list.remove(max(res_list))  
    11.   
    12. #printing the second largest element   
    13. print(max(res_list))  

    Output:

    30
    

    Explanation -

    Let us understand what we have done in the above program-

    1. We have declared the list from which we want to take out the second last element.
    2. After this, we used the set method to take all the unique elements of the list.
    3. Now we make use of max() to get the maximum value from the list and then remove it.
    4. After this, we print the maximum of the resultant list which will give us the second-largest number.

    In the third method, we will use for loop and find the second largest number from the list.

    Example -

    1. # declaring empty list  
    2. list_val = []  
    3.   
    4. # user provides the number of elements to be added in the list  
    5. num_list = int(input("Enter number of elements in list: "))  
    6.   
    7.   
    8. for i in range(1, num_list + 1):  
    9.     element = int(input("Enter the elements: "))  
    10.     list_val.append(element)  
    11.   
    12.   
    13. # sort the list  
    14. list_val.sort()  
    15.       
    16. # print second largest element  
    17. print("Second largest element is:", list_val[-2])  

    Output:

    Enter number of elements in list: 5
    
    Enter the elements: 10
    
    Enter the elements: 20
    
    Enter the elements: 30
    
    Enter the elements: 40
    
    Enter the elements: 50
    The second largest element is: 40
    

    Explanation -

    Let us have a glance at what we have done here-

    1. We have declared an empty list in which we will insert the elements.
    2. After this, we ask the user to provide us the number of elements we would like to add to our list.
    3. After this, we use the sort method so that all the elements of our list are arranged in ascending order.
    4. Now we make use of negative indexing since the second-largest number will come at the second last position.

    Traversing the list

    In the last program, we will traverse the list to find out the largest number and then make use of conditional statements to find the second largest number from the list.

    The following program illustrates the same-

    Example -

    1. def calc_largest(arr):  
    2.     second_largest = arr[0]  
    3.     largest_val = arr[0]  
    4.     for i in range(len(arr)):  
    5.         if arr[i] > largest_val:  
    6.             largest_val = arr[i]  
    7.   
    8.     for i in range(len(arr)):  
    9.         if arr[i] > second_largest and arr[i] != largest_val:  
    10.             second_largest = arr[i]  
    11.   
    12.     return second_largest  
    13. print(calc_largest([2030402510]))  

    Output:

    30
    

    Explanation -

    Let us understand what we have done in the above program-

    1. The first step is to create a function that checks the largest number from the list by traversing it.
    2. In the next for loop, we traverse the list again for finding the highest number but this time excludes the previous one since here our objective is to find the second largest function.
    3. Finally, we pass our list in the function.

    So, in this article, we got the chance to think out of the box and discover some new ways to develop the logic for finding the second largest number in Python.







    Youtube For Videos Join Our Youtube Channel: Join Now

    Feedback

    • Send your Feedback to feedback@javatpoint.com

    Help Others, Please Share

    facebook twitter pinterest

    Learn Latest Tutorials


    Preparation


    Trending Technologies


    B.Tech / MCA